Re: Proposal: new Symbol(obj)
(a === b) = (new Symbol(a) === new Symbol(b)) Not sure I follow... so, I either I don't agree or I don't understand :). I'm having to dig deep to remember my math vocab here, but I think it may be most correct to say that the Symbol constructor, when passed an argument, should be injective [1]. That is, 1. let *F* = *new Symbol* 2. if http://en.wikipedia.org/wiki/Material_conditional *a* = *b*, then *F *(*a**)* = *F(b)* 3. if *a* != *b*, then *F(a) *!= *F(b)* In any case, I'd also say that weak maps are good enough for your use case. In some (most?) cases, but not all. There's already a consensus that the garbage collection semantics of WeakMaps aren't always appropriate [2]. By parameterizing the Symbol constructor, developers can create custom map/set types without the overhead of a Symbol Factory (as previously suggested by Allen). I believe this would be a useful building block for interesting and innovative custom types. [1]: https://en.wikipedia.org/wiki/Injective_function#Definition [2]: http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets On Mon, Jul 15, 2013 at 5:52 AM, Andreas Rossberg rossb...@google.comwrote: On 12 July 2013 22:29, Jeremy Martin jmar...@gmail.com wrote: My expectation would be that... (a === b) === (new Symbol(a) === new Symbol(b)) Nit: you probably either mean (a === b) = (new Symbol(a) === new Symbol(b)) or (Object.is(a, b)) === (new Symbol(a) === new Symbol(b)) In any case, I'd also say that weak maps are good enough for your use case. /Andreas -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
so, I either I don't agree or I don't understand It was the latter - I understand and agree now :) On Mon, Jul 15, 2013 at 10:03 AM, Andreas Rossberg rossb...@google.comwrote: On 15 July 2013 15:49, Jeremy Martin jmar...@gmail.com wrote: (a === b) = (new Symbol(a) === new Symbol(b)) Not sure I follow... so, I either I don't agree or I don't understand :). I'm having to dig deep to remember my math vocab here, but I think it may be most correct to say that the Symbol constructor, when passed an argument, should be injective [1]. That is, 1. let F = new Symbol 2. if a = b, then F(a) = F(b) 3. if a != b, then F(a) != F(b) I agree, but the problem is that JavaScript's === is not an equivalence relation, due to the dreaded NaN !== NaN that IEEE invented in some delirium. So you cannot define injectivity based on it. You merely get an implication for the above, which is what the = was supposed to encode. Object.is OTOH implements a proper equivalence relation, i.e. a = in the mathematical sense. It only differs from === by having a sane semantics for NaN. But as I said, I was merely picking a nit. /Andreas -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
On Jul 15, 2013, at 6:49 AM, Jeremy Martin wrote: In some (most?) cases, but not all. There's already a consensus that the garbage collection semantics of WeakMaps aren't always appropriate [2]. By parameterizing the Symbol constructor, developers can create custom map/set types without the overhead of a Symbol Factory (as previously suggested by Allen). I believe this would be a useful building block for interesting and innovative custom types. [1]: https://en.wikipedia.org/wiki/Injective_function#Definition [2]: http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets An implementation of your proposal is going to have to have to internally use some wort of weak-keyed table, in practice an implementation would probably use the same GC mechanisms that are there to support WeakMap. So, I doubt there would be much performance difference between a built-in and a roll-your-own implementation. Allen ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
The builtin could have weak values (i.e. Symbol instances expire when no longer referenced by JS) instead of weak keys, which is not something we can currently express in JS. This would also make it possible to use strings/integers/floats as Symbol keys without leaking those Symbol instances forever. This is not something we can express in JS either. Both of these behaviors would not, as I understand it, be directly observable since keeping an old Symbol instance around (to compare with) would prevent the cached one from being collected. On the other hand, I think if you were to resynthesize the number/string used as a symbol key and make a new symbol, that might allow you to observe whether the symbol had been collected. This doesn't seem like a huge problem to me but I forget the exact reasoning why weak references were unacceptable in the past; maybe it still applies to this. On Mon, Jul 15, 2013 at 9:08 AM, Allen Wirfs-Brock al...@wirfs-brock.comwrote: On Jul 15, 2013, at 6:49 AM, Jeremy Martin wrote: In some (most?) cases, but not all. There's already a consensus that the garbage collection semantics of WeakMaps aren't always appropriate [2]. By parameterizing the Symbol constructor, developers can create custom map/set types without the overhead of a Symbol Factory (as previously suggested by Allen). I believe this would be a useful building block for interesting and innovative custom types. [1]: https://en.wikipedia.org/wiki/Injective_function#Definition [2]: http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets An implementation of your proposal is going to have to have to internally use some wort of weak-keyed table, in practice an implementation would probably use the same GC mechanisms that are there to support WeakMap. So, I doubt there would be much performance difference between a built-in and a roll-your-own implementation. Allen ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Proposal: new Symbol(obj)
In brief: allow Symbol's to be constructed with a single parameter, with the following behavior: var obj = {}; undefined new Symbol({}) === new Symbol({}) false new Symbol(obj) === new Symbol(obj) true Motivation: the ability to construct equal Symbols gives us the necessary building blocks to build custom maps with semantics very similar to Simple Maps[1]: function ObjectMap() { this.map = {}; } SimpleMap.prototype.set = function(objKey, val) { this.map[new Symbol(objKey)] = val; }; SimpleMap.prototype.get = function(objKey) { return this.map[new Symbol(objKey)]; }; At a surface level, this seems more novelty than anything else, but I think it's a useful primitive for building more complex and robust features on top of Symbols. Thoughts? [1] http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
On Jul 12, 2013, at 12:14 PM, Jeremy Martin wrote: In brief: allow Symbol's to be constructed with a single parameter, with the following behavior: var obj = {}; undefined new Symbol({}) === new Symbol({}) false new Symbol(obj) === new Symbol(obj) true You can use a WeakMap to build your own object-selected Symbol factory: let known = new WeakMap; function ObjSymFactory(obj) { //handle obj === undefined however you want let sym = known.get(obj); If (!sym) { sym = new Symbol; known.set(obj,sym); } return sym; } Allen ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
Good point, that's definitely a usable solution (also a better representation of what I was attempting to describe). I'd still be interested in a less-verbose/more-efficient approach using the Symbol constructor, but it may not be a common enough scenario to justify it when a workaround does exist. On Fri, Jul 12, 2013 at 3:32 PM, Allen Wirfs-Brock al...@wirfs-brock.comwrote: On Jul 12, 2013, at 12:14 PM, Jeremy Martin wrote: In brief: allow Symbol's to be constructed with a single parameter, with the following behavior: var obj = {}; undefined new Symbol({}) === new Symbol({}) false new Symbol(obj) === new Symbol(obj) true You can use a WeakMap to build your own object-selected Symbol factory: let known = new WeakMap; function ObjSymFactory(obj) { //handle obj === undefined however you want let sym = known.get(obj); If (!sym) { sym = new Symbol; known.set(obj,sym); } return sym; } Allen -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
I would welcome (with fanfare and parades) a new Symbol(obj) that worked for strings and integers. Such is not possible using the WeakMap shim (you'd have to detect the type of the value and have multiple dictionaries, or something, and you'd leak the symbols forever...) Of course, what that means is I'm asking for weakly cached symbols with referential identity, which means it would allow detecting garbage collections, so this is probably around the point where people chime in and say it's not possible. Oh well. :( On Fri, Jul 12, 2013 at 12:42 PM, Jeremy Martin jmar...@gmail.com wrote: Good point, that's definitely a usable solution (also a better representation of what I was attempting to describe). I'd still be interested in a less-verbose/more-efficient approach using the Symbol constructor, but it may not be a common enough scenario to justify it when a workaround does exist. On Fri, Jul 12, 2013 at 3:32 PM, Allen Wirfs-Brock al...@wirfs-brock.comwrote: On Jul 12, 2013, at 12:14 PM, Jeremy Martin wrote: In brief: allow Symbol's to be constructed with a single parameter, with the following behavior: var obj = {}; undefined new Symbol({}) === new Symbol({}) false new Symbol(obj) === new Symbol(obj) true You can use a WeakMap to build your own object-selected Symbol factory: let known = new WeakMap; function ObjSymFactory(obj) { //handle obj === undefined however you want let sym = known.get(obj); If (!sym) { sym = new Symbol; known.set(obj,sym); } return sym; } Allen -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: new Symbol(obj)
My expectation would be that... (a === b) === (new Symbol(a) === new Symbol(b)) I.e., `new Symbol(a) === new Symbol(b)` iff `a === b`. This satisfies the strings/integers scenario, but, of course, fails your WeakMap garbage collection semantics. You need WeakSymbolMaps (+ this proposal) :) On Fri, Jul 12, 2013 at 4:15 PM, K. Gadd k...@luminance.org wrote: I would welcome (with fanfare and parades) a new Symbol(obj) that worked for strings and integers. Such is not possible using the WeakMap shim (you'd have to detect the type of the value and have multiple dictionaries, or something, and you'd leak the symbols forever...) Of course, what that means is I'm asking for weakly cached symbols with referential identity, which means it would allow detecting garbage collections, so this is probably around the point where people chime in and say it's not possible. Oh well. :( On Fri, Jul 12, 2013 at 12:42 PM, Jeremy Martin jmar...@gmail.com wrote: Good point, that's definitely a usable solution (also a better representation of what I was attempting to describe). I'd still be interested in a less-verbose/more-efficient approach using the Symbol constructor, but it may not be a common enough scenario to justify it when a workaround does exist. On Fri, Jul 12, 2013 at 3:32 PM, Allen Wirfs-Brock al...@wirfs-brock.com wrote: On Jul 12, 2013, at 12:14 PM, Jeremy Martin wrote: In brief: allow Symbol's to be constructed with a single parameter, with the following behavior: var obj = {}; undefined new Symbol({}) === new Symbol({}) false new Symbol(obj) === new Symbol(obj) true You can use a WeakMap to build your own object-selected Symbol factory: let known = new WeakMap; function ObjSymFactory(obj) { //handle obj === undefined however you want let sym = known.get(obj); If (!sym) { sym = new Symbol; known.set(obj,sym); } return sym; } Allen -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss -- Jeremy Martin 661.312.3853 http://devsmash.com @jmar777 ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss