||= is much needed?

T.J. Crowder tj at crowdersoftware.com
Thu Jun 14 10:26:24 PDT 2012

On 14 June 2012 18:10, Brendan Eich <brendan at mozilla.com> wrote:

> I agree on reflection with Wes and others who've objected that A ?: B has
> the simplest interpretation as A ? A : B and therefore should not be used
> for anything like (A !== undefined) ? A : B or (A != null) ? A : B. I noted
> this as an open issue but I'm almost ready to flip the strawman back to ??
> and ??=. Comments on syntax?

Do people see sufficient value in a second ternary operator that uses the
same semantics for what's a non-value? E.g.:

a = b ?? c : d;


a = b !== undefined ? c : d;

If people *don't* see sufficient value in the second ternary (and the use
cases are pretty limited), ?? and ??= are great. Ship 'em.

If people *do* see sufficient value there, ?? is probably better suited to
that than then the infix operator we're talking about, and the infix
version will want a new name. ??: perhaps, which is easy to type
and harkens to the second ternary. That would give us:

a = b ?? c : d;
a ??= b;
a = b ??: c;

I quite like the full set, but if it's just me, that's a non-argument.

On semantics: CoffeeScript's use of != null to equate null and undefined
> matches some users' habits, and helps to conceal the awkward fact of two
> bottom-types in JS (@jashkenas just averred that was the goal). But such
> concealment can never be perfect and other users will either want to
> distinguish, or (what is more worrisome) will accidentally distinguish
> (e.g. with !==) null from undefined.

FWIW: I never use null to mean "default me" in JS, and have never seen
anyone do so. That's just my anecdotal experience. A couple of people have
posted that they do, which is their anecdotal experience.

I would rather the new operators just considered undefined as the
non-value, not both undefined and null. But it wouldn't impact my code if
it did (because I never use null in this way). It's just a preference.

-- T.J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120614/95978e8c/attachment-0001.html>

More information about the es-discuss mailing list