4 ms·
It's too bad that JavaScript collections have ugly Java-like APIs, with get(k), set(k, v), etc. It would be nice if the [] and . weren't essentially aliases fo
by spankalee 11y ago
It's too bad that JavaScript collections have ugly Java-like APIs, with get(k), set(k, v), etc.
It would be nice if the [] and . weren't essentially aliases for each other, so that [] could be use for collections. There was once a proposal for this, though it would have broken assumptions of lots of code: http://wiki.ecmascript.org/doku.php?id=strawman:object_model_reformation http://wiki.ecmascript.org/doku.php?id=strawman:object_model...
- TazeTSchnitzel 11y agoSounds like a bad idea, [] and . have always acted directly on object properties. Changing the semantics of a basic language operator can have consequences you wouldn't envision. What if an existing function deals with a new object? Add a new operator, like {}.
- pcwalton 11y agoI think using curly braces would be an automatic semicolon insertion hazard. You would at least need to introduce another "no LineTerminator here" rule.
- TazeTSchnitzel 11y agoSure, there might be a better syntactical choice, though I'm not sure what it would be.
- xiaq 11y agoUse map(key). I learnt this "map as callable" idea fron clojure and wondered why it is not more common.
- epidemian 11y agoYeap. I find it very elegant to be able consider maps as simply functions that map elements from one domain to another: a -> b. A set can then simply be consider a special case of map: a -> Bool. A definition like that can also make concepts like infinite sets much easier to conceive. It'd be nice to be able to express "the set of all prime numbers" or "a map between a person and their soul-mate: Peson -> Maybe(Person)" and have them be interchangeable with other Sets and Maps (of same type) on general operations like intersection, difference, etc. But i think this interpretation is at odds with the idea of maps and sets being data structures of a given size, where you add and remove things.
- pekk 11y ago> [] and . have always acted directly on object properties. That was the bad idea, but as you suggest it really can't be fixed now.
- TazeTSchnitzel 11y agoIt's not at all a bad idea. It means you can look up properties on an object by expression, but gives you a short syntax for the common case.