Existential operator (was: ||= is much needed?)
Brendan Eich
brendan at mozilla.org
Tue Jun 19 11:58:03 PDT 2012
That's possible but it is a bit ugly, isn't it?
Also, as dherman observed, ?. and variations are different from
defaulting, in one point of view. That is, avoiding dereferencing a
value that does not coerce to object is the purpose of ?. but not
necessarily of ??= (or || as used for defaulting today).
We could use ?obj as a prefix form for testing whether obj is not null
or undefined, but it would then lead to doubled ?? when put in the
"then" part of a ?: ternary.
Using ?? as a suffix form is even uglier in a ternary: obj???wut:lol.
We already have obj != null for testing whether obj is not null or
undefined, so that takes away a lot of the pressure for unary ??.
My current thinking is:
(a) ?? and ??= as proposed in
http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
(undefined-only).
(b) ||= as a freebie since || is in the language and useful for
falsy-defaulting (latent bugs notwithstanding).
(c) ?. as proposed in
http://wiki.ecmascript.org/doku.php?id=strawman:existential_operator.
(d) obj.maybeMethod?.(args) for what CoffeeScript calls ?(.
/be
Domenic Denicola wrote:
> What struck me from Jeremy's explanation was the uniformity of CoffeeScript's ? operator (modulo the small issue he mentioned for function calls). It seems to combine the currently-strawman'ed ??/??= and ?. into one very simple semantic.
>
> What about adopting ?? as CoffeeScript's ? operator? Would this solve the lookahead problems? So you could do
>
> object.property??.other.properties.go.here
> object.method??(args)
>
> This would unfortunately imply, for uniformity, that
>
> object ?? other
> object ??= other
>
> become null + undefined tests instead of just undefined. But that might be worth paying.
>
> For the record that leaves object?? as the only unimplemented CoffeeScript counterpart.
More information about the es-discuss
mailing list