Existential operator (was: ||= is much needed?)
Brendan Eich
brendan at mozilla.org
Wed Jun 20 13:45:09 PDT 2012
Herby Vojčík wrote:
> Let's allow foo.? to denote "soft foo", so you get:
>
> foo.?.bar // foo == null ? undefined : foo.bar
> foo.?(bar) // foo == null ? undefined : foo(bar)
> foo.?[bar] // foo == null ? undefined : foo[bar]
?. works as well for these and the main (by use-frequency, so far) form
matches CS. If we must have extra dots in the lesser-used forms, I'd put
the dot in the middle to keep the question-mark on the left.
> but maybe also more esoteric uses, like:
>
> for (let key in foo.?) // for (let key in (foo == null ? {} : foo))
That is even more of an abuse of dot, and it does not play well with ?:
ternaries and the proposed ?? operator.
foo.??bar:baz
would be legal by maximal munch, parsed as
(foo.?)?bar:baz.
Beyond this objection, the need for foo.? instead of foo != null is not
strong.
Worse, you show magic defaulting to {} in the for-in case, but the
default result for anything like a suffix-? expression in CS is a
boolean boolean result:
$ cat /tmp/w.coffee
lhs = foo?
$ ./bin/coffee -p /tmp/w.coffee
(function() {
var lhs;
lhs = typeof foo !== "undefined" && foo !== null;
}).call(this);
> foo.? // foo == null ? undefined : foo // sort-of clearing
See above.
> foo.? = bar // foo == null ? undefined : (foo = bar)
> // this semantics is logical, just don't know
> // what would be the use case...
> foo.?.baz = bar // foo == null ? undefined : (foo.baz = bar)
> // this is usable
>
These don't win over ?. in the proposal.
/be
More information about the es-discuss
mailing list