{Weak|}{Map|Set}

Kam Kasravi kamkasravi at yahoo.com
Wed Sep 14 23:47:09 PDT 2011


The reference to private is actually in the Map, Set classes shown below. The BNF is also below:

CallExpression : ... private ( AssignmentExpression ) ConstructorElement : ... PrivateVariableDefinition PrivateVariableDefinition : private ExportableDefinition
class Map { private keys, vals; constructor() { private(this).keys = []; private(this).vals = []; } get(key) { const keys = private(this).keys; const i = indexOfIdentical(keys, key); return i < 0 ? undefined : private(this).values[i]; } has(key) { const keys = private(this).keys; return indexOfIdentical(keys, key) >= 0; } set(key, val) { const keys = private(this).keys; const vals = private(this).vals; let i = indexOfIdentical(keys, key); if (i < 0) { i = keys.length; } keys[i] = key; vals[i] = val; } delete(key) { const keys = private(this).keys; const vals = private(this).vals; const i = indexOfIdentical(keys, key); if (i < 0) { return false; } keys.splice(i, 1); vals.splice(i, 1); return true; } // todo: iteration }
class Set { private map; constructor() { private(this).map = Map(); } has(key) { return private(this).map.has(key); } add(key) { private(this).map.set(key, true); } delete(key) { return private(this).delete(key); } // todo: iteration }

________________________________
From: Kam Kasravi <kamkasravi at yahoo.com>
To: Mark S. Miller <erights at google.com>
Cc: es-discuss <es-discuss at mozilla.org>
Sent: Wednesday, September 14, 2011 7:53 PM
Subject: Re: {Weak|}{Map|Set}


I noticed that these class definitions declare private names within the class body but not the constructor. My latest read of the class proposal was that private could only be declared within the constructor. Have I interpreted the BNF incorrectly?

On Sep 14, 2011, at 6:20 PM, "Mark S. Miller" <erights at google.com> wrote:


On Wed, Sep 14, 2011 at 6:04 PM, Juan Ignacio Dopazo <dopazo.juan at gmail.com> wrote:
>
>On Wednesday, September 14, 2011, David Bruant <david.bruant at labri.fr> wrote:
>>> Also, I would like to talk a little bit about terminology. WeakMaps have
>>> their name inspired by the idea of "weak" references which have
>>> particular garbage-collection properties. From the developer
>>> perspective, this seems to be some sort of implementation detail they
>>> should not be aware of.
>>> As far as I know, current functions/constructors have their name
>>> inspired by the contract they fulfill rather than implementation
>>> considerations. The difference between current WeakMaps and Maps is
>>> their contract. In the latter, keys can be enumerated, in the former
>>> not. I think that this is the difference that should inspire different
>>> names rather than the implementation optimisation that is induced by
>>> this contract difference.
>>>
>>
>>
>>In the last few days I had to write a piece of code that would strongly benefit from WeakMaps. I needed to store information about DOM nodes and retrieve it later, and these nodes aren't in my control so they can be detached at any time by someone else. If the references I kept were weak, I'd be sure that I wouldn't be causing a memory leak. And that's important in this case because the nodes are very likely Flash objects which can easily mean 20-50mb in memory. So knowing that a reference is weak is important information.
>
>
>I agree. 
>
>
>Normally I strongly take the same position David does: emphasize semantics over implementation. But why? It is good when we can label a tool according to its purpose, rather than how it accomplishes that purpose. Associating the tool with its purpose helps us remember the right tool for the right job. Few would reach for the WeakMap tool thinking "I need a non-enumerable table". Granted, there are cases when the non-enumerability is the desired feature, but those cases are rare. The common purpose of a WeakMap is rooted in our understanding, at a high level, of certain implementation costs, and our desire to avoid certain avoidable implementation costs. Generally, that is what a WeakMap is *for*.
>
>-- 
>    Cheers,
>    --MarkM
>
_______________________________________________
>es-discuss mailing list
>es-discuss at mozilla.org
>https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110914/899c3988/attachment-0001.html>


More information about the es-discuss mailing list