[webidl] Add a map type (#70)

I'm not sure there is enough critical mass for this yet, but I've seen this come up a few times, so I thought I'd throw it out there...

Some APIs would like to accept a JavaScript object of the form:
```js
   let optionmap = {
      key1 : "stringValue1",
      key2 : "stringValue2",
      keyN : "stringValueN"
   };
   thing.doSomething( optionmap );
```
Normally, this would be modelled as a dictionary of the form:
```idl
   dictionary OptionMap {
      DOMString key1;
      DOMString key2;
      DOMString keyN;
   };
   void doSomething( OptionMap optionmap );
```
However what is really wanted is not a fixed-length set of dictionary members, but rather an open-ended map of key/value pairs, where the map matches a particular pattern, e.g., in the above example where all keys are a ```DOMString``` with a particular unique value, and the value of each key is a ```DOMString```. Another example is the object provided as the second parameter to the built-in ```Object.create``` method (a map where the key is the property name and the value is a property descriptor).

I realize that there are other potential "advanced" things that might be desired, such as combining normal dictionary semantics with generic map semantics, and while I think that is interesting, I've not encountered a request for it.

I'm looking for a Type that can be declared so that JS objects can be converted to matching IDL and vice-versa. Re-using the existing 'maplike' declaration, the type could be as simple as:
```js
   map<T> // if we constrain keys to be DOMString
   // or
   map<keyType, T> // If the map's keys can be any type
```
To wrap up, my initial JavaScript example could be expressed as follows, given the above ```map<T>``` type:
```idl
   void doSomething( map<DOMString> optionmap );
```


---
Reply to this email directly or view it on GitHub:
https://github.com/heycam/webidl/issues/70

Received on Thursday, 29 October 2015 06:13:29 UTC