Separating a Hash type from Object

Brendan Eich brendan at mozilla.org
Wed May 23 13:59:03 PDT 2007


On May 23, 2007, at 6:11 PM, Brad Fults wrote:

> Assuming that your proposal is for actual end use of dicts and not
> just implementation details, very simply, that's far more complex
> than:
>
>     var d = {a: 1, b: 2};

That's already defined by ES3 and can't be changed compatibly.

> and therefore won't be used as much and will be abused gratuitously.
> In the eyes of the novice (and in JS even the intermediate developer)
> your proposal looks something like:
>
>     voodoo voodoo voodoo
>     let dict = voodoo{a:1, b:2, ...};
>
> I don't mean to be caustic, I simply mean to represent the novice in a
> case that I think should be accessible to all developers
> (dicts/hashes).

Since we cannot redefine object initialiser syntax, I'm not sure what  
alternative you would prefer. It's one thing to say "we need a  
Dictionary nominal type". It's another to say "we want incompatible  
change to object initialiser syntax". The latter will get you nowhere  
fast. The former may be good for some cases, but it runs into the  
weak keys or ephemerons issue quickly.

> It also has the notable pitfall of applying the
> namespace to the entire code block, something that would cause even
> more confusion among novice developers.

How so?

> Would it be at all possible to handle the key safety in the Dictionary
> type itself (a bit of magic), leaving us with something like:
>
>     let dict:Dictionary = {a:1, b:2, ...};
>
> This simpler and more intuitive syntax would (I think) be *much* more
> likely to be used and not abused by all levels of developers. Though
> if this syntax were feasible I'd advocate for naming the type either
> Dict or Hash for brevity's sake:
>
>     let myHash:Hash = {a:1, b:2, ...};

Sure, we've been over this ground. It can be done with something like  
catchalls and a private namespace, *in ES4 itself* (no native magic  
required). If you ignore the weak reference questions that it begs,  
the only remaining challenge for ES4 is to agree on the API.

> So, {a:1} would still represent an Object literal, but when a type
> hint of Dict/Hash is used it becomes a Dict/Hash initializer which has
> key safety.
>
> Thoughts? Let me know if I'm completely off base.

I think you are right to argue that a short type annotation is better  
than both a private namespace and a u:: in front of the opening  
brace. But how much better, without other support such as weak keys  
(and values) and API elaboration that is probably best left to the  
library ecosystem.

We aren't going to fit weak refs into ES4, so if we can converge on  
the minimal API and implement it using namespaces and catchalls (not  
hashcode and chained hash tables implemented on top of objects!),  
then it perhaps could make ES4 on a wing and a prayer.

/be




More information about the Es4-discuss mailing list