On 13.04.2011 21:57, Brendan Eich wrote:
On Apr 13, 2011, at 3:38 PM, Kyle Simpson wrote:

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 (?:).
The "default operator" doesn't address a significant part of what Dmitry is 
asking for -- the . in the ?. usage -- which allows the property access to be expressed 
only once and used for both the test and assignment.
This was a one-line FYI, and on-topic in reply to Dmitry's post since he 
brought up ?= (spelled ??= in the strawman). Why are you objecting to it?

Moving right along...


let street = user.address?.street

which desugars e.g. into:

street = (typeof user.address != "undefined"&&  user.address != null)
   ? user.address.street
   : undefined;
Part of what Dmitry asked for, I'd like to see in the plain ?: operator, and it seems 
like it would be possible to disambiguate from a top-down parser perspective. I would 
like to see the `:` ("else condition") portion of a ?: expression be optional. 
For instance:

var a = b ? c;  // aka, `var a = b ? c : undefined`

The other (more awkward/obscure looking) way to do this is:

var a;
b&&  a = c;
First, making : optional introduces a dangling-else ambiguity:

   x = a ? b ? c : d;

This could be (x = a ? (b ? c : d)) or (x = a ? (b ? c) : d).

True, if-else already has this (traditional in C-based languages) ambiguity, 
"resolved" by associating : with the inner ? and so requiring the programmer to 
use braces if the other association is wanted, or in general just to avoid trouble. But 
why should we add more of the same kind of ambiguity, given the benefit of hindsight?


Yeah, right, no new ambiguities are needed.

Second, as I see Garrett asked in followup, why not simply write:

   var a = b&&  c;

If b is falsy but not undefined, and you don't want its falsy value propagating 
to a, then of course you'll have to write out:

   var a = b ? c : undefined;

or:

   var a;
   if (b) {
     a = c;
   }

or equivalent.


Yes, or (which today is used as a better alternative of the default value):

b && (a = c);

(though, this doesn't assign `undefined` to `c` in case of failur)

This approach is used to avoid unnecessary assignment to itself. Instead of:

foo = foo || default;

better replacement is:

foo || (foo = default);

Another example:

Array.prototype.map || Array.prototype.map = function () {
  /* map implementation */
};

And of course from this viewpoint special default operator (again, no matter much in which exactly syntactic form -- ??, ?=, ??=, ||=, etc) is good.

Dmitry.

but this seems like a hard case, and as they teach first year law students, 
those make bad law.


The difference between the sytnax sugar I'm asking for and the "default operator" in 
the strawman is that ?: (or&&) allows for separate expressions for the test (`b`) and the 
success_value (`c`), whereas ?? requires that the test expression and success_value be the same 
expression.

For instance:

var a = (b>  5) ? b : undefined;

In this case, the ?? "default operator" is of no use. But being able to drop the `: 
undefined` part, and also avoid using the more awkward looking&&  syntax, would certainly 
be a useful sugar.
I think the dangling else problem is enough to nix this, but I welcome other 
comments. I'm also not sure b is falsy but not undefined in practice. It seems 
contrived to want a numeric value sometimes, and undefined others. IOW,

   var a = (b>  5) ? b : undefined;

looks like a bug that will result in "undefined" or NaN values propagating via a, some of 
the time, into other numeric or number to string expressions. It looks like a mis-coded 
"min" against 5.

I don't see any ?. use case here, so I'm still not sure what this has to do 
with Dmitry's post or my reply. The topic in the Subject: line is 
CoffeeScript's existential operator, not anything that might be spelled with a 
? -- but it's ok, we can discuss. Just please don't take me to task for citing 
a relevant strawman in reply to someone else's post that brought up (among 
several things) exactly what the strawman proposes, namely ??=.

/be

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

Reply via email to