See http://wiki.ecmascript.org/doku.php?id=strawman:default_operator -- the 
proposal there is ?? and ??= since single ? is ambiguous after an expression 
due to conditional expressions (?:).

On Apr 13, 2011, at 11:37 AM, Dmitry A. Soshnikov wrote:

> let street = user.address?.street
> 
> which desugars e.g. into:
> 
> street = (typeof user.address != "undefined" && user.address != null)
>    ? user.address.street
>    : undefined;

(Just user.address != null would be enough, although perhaps there's a reason 
to typeof-test for undefined first that I am missing.)


> The same with functions (which is even more convenient that just with 
> properties):
> 
> let value = entity.getBounds?().direction?.x
> 
> which desugars into:
> 
> let x = (typeof entity.getBounds == "function")
>    ? (typeof entity.getBounds().direction != "undefined" && 
> entity.getBounds().direction != null)
>        ? entity.getBounds().direction.x
>        : undefined
>    undefined;
> 
> (I specially avoid optimization with saving intermediate results -- just to 
> keep clarity)

(Let's hope getBounds has no side effects :-P)

The ?. form from CoffeeScript is attractive at first glance. For notifying an 
ad-hoc observer where the reference to the observer may be null or undefined, 
it saves an 'if'. But for deeper optional control flow you need an 'if' anyway, 
in my experience.

How much is this used in CoffeeScript compared to regular .? Some crude 
measurement would be helpful.

/be
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to