ES4 draft: Map

Lars Hansen lhansen at adobe.com
Sun Mar 2 23:26:32 PST 2008


Following up to myself: 

> -----Original Message-----
> From: Lars Hansen 
> Sent: 3. mars 2008 08:04
> To: 'Waldemar Horwat'
> Cc: es4-discuss at mozilla.org
> Subject: RE: ES4 draft: Map
> 
> > -----Original Message-----
> > From: Waldemar Horwat [mailto:waldemar at google.com]
> > Sent: 1. mars 2008 01:53
> >
> > Why does get return null instead of undefined when it fails 
> > to find an instance?
> 
> No good reason that I can think of.  I think undefined is at 
> least as sensible; will fix.

Actually, it was for Java compatibility (the API is modelled on
Java, but imperfectly).  No matter.

> > A version of get with a second parameter X that returns X when the 
> > value isn't present would be useful.
> 
> I agree, and that parameter could be optional and default to 
> undefined.  Will fix.

There is actually a problem with an arbitrary optional parameter, 
and the problem also comes up if -- as somebody sent me private
mail about -- we want put() to return the previous value for the
key, if there is one.  For strict mode we probably would like
get() to be declared to returning V or at most (V|undefined).
So X would be constrained likewise.

In summary, this seems reasonably simple:

  function get(key:K, default:(V|undefined)=undefined):(V|undefined) ...

  function put(key:K, value:V,
default:(V|undefined)=undefined):(V|undefined) ...

with the proviso that if the table may associate K with the
value undefined then the programmer has to be careful and
use "has".

--lars



More information about the Es4-discuss mailing list