Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Should null-coalescing operator be included in the proposal? #9

Closed
claudepache opened this issue Jun 28, 2017 · 19 comments
Closed

Should null-coalescing operator be included in the proposal? #9

claudepache opened this issue Jun 28, 2017 · 19 comments

Comments

@claudepache
Copy link
Collaborator

a ?? b evaluates to a if a is not null/undefined, b otherwise (with same short-circuiting semantics than || and &&).

Technically, this is completely orthogonal to ?.. However, it is semantically related.

@gisenberg
Copy link
Member

I'm planning on bringing this up as a stage-0 proposal at the next TC39 meeting. During initial discussions of ?., the use of foo?.bar || 'some-default-value' was strongly discouraged and has pitfalls.

Null coalescing didn't seem like the right name for something that also checks for undefined, though.

@ljharb
Copy link
Member

ljharb commented Jun 28, 2017

I like ?? but I think it needs to be a separate proposal - it doesn't come with any of the object lookup pitfalls or method invocation pitfalls that this one does.

Obviously both proposals would need to agree on "undefined only" or "null or undefined", but that's a stage 2 concern.

@gisenberg
Copy link
Member

I'll be treating it as a separate proposal and will be seeking stage 1. I'll link the PR to the TC39 agenda in this issue.

RE: "null or undefined", I have some strawman arguments for why checking against both is worthwhile.

@claudepache
Copy link
Collaborator Author

Null coalescing didn't seem like the right name for something that also checks for undefined, though.

Alternative names:

  • default operator;
  • defined-or operator; probably not suitable if it checks for undefined and null.

But “null-coalescing” is OK for me (if we choose the “undefined or null” semantics), given that undefined == null holds. It has the advantage of making clear that it checks also for null, not just undefined, in contrast with defaults in function parameters and destructuring.

@ljharb
Copy link
Member

ljharb commented Jun 28, 2017

The term i use for "undefined or null" is "nullary"; I've not seen a more precise term elsewhere.

@jridgewell
Copy link
Member

During initial discussions of ?., the use of foo?.bar || 'some-default-value' was strongly discouraged and has pitfalls.

Can you describe this more? Or link to it if it's in the notes?

Is this just because || won't treat the propagated "nil" different than normal null/undefined/false/all-the-rest?

@gisenberg
Copy link
Member

@jridgewell Here are my rough notes on the syntax. The short version is that ||'s behavior means you can't get optional values out if those values are falsey. Consider:

const obj = {
  foo: {
    bar: {
      baz: false // using 'false' for illustrative purposes, also applies to 0, empty strings, and other falsey values.
    }
  }
}

obj?.foo?.bar?.baz || true // always returns true, will never assign provided value
obj?.foo?.bar?.baz ?? true // returns false, nullary coalescing operator likely matches developer intent

@Mouvedia
Copy link

Mouvedia commented Jun 28, 2017

Once this proposal has a dedicated repository, this issue should be closed.

Are there other languages that have something similar to ?? ? If so what are the differences ?

@jsommr
Copy link

jsommr commented Jul 1, 2017

C# has ?? and it acts the same as suggested here.

@claudepache
Copy link
Collaborator Author

Are there other languages that have something similar to ?? ? If so what are the differences ?

For prior art, see https://en.wikipedia.org/wiki/Null_coalescing_operator

One significant difference is precedence relatively to other operators, see claudepache/es-optional-chaining#10 (comment)

@dotnetwise
Copy link

@ljharb falsy, truthy, nully could be in the same range

@ljharb
Copy link
Member

ljharb commented Jul 15, 2017

falsy includes false, the empty string, zero, and NaN (and truthy is everything that's not falsy) - nully could work but i've never seen that one before.

@rattrayalex
Copy link

rattrayalex commented Jul 15, 2017

@gisenberg do you plan to submit the ?? proposal, or are you aware of another TC39 member who might?

@rattrayalex
Copy link

Regarding terminology, lodash uses isNil to check for null or undefined, so I have taken to using the term nil for this. However, this is somewhat confusing to those coming from languages where null is spelled nil, such as Ruby.

I have also used the term nullish in the past, usually to little confusion.

@ljharb
Copy link
Member

ljharb commented Jul 15, 2017

@rattrayalex only tc39 members can submit proposals, ftr.

@rattrayalex
Copy link

rattrayalex commented Jul 15, 2017

Ah, that's right – thanks for the reminder @ljharb . I will amend my comment accordingly.

@gisenberg
Copy link
Member

@rattrayalex Yes, I intend to submit the proposal.

@littledan
Copy link
Member

I like the idea of pursuing this feature. Either together or separate could be good; the important thing is that they respect the same choice of null/undefined vs just undefined.

@littledan littledan mentioned this issue Jul 18, 2017
@gisenberg
Copy link
Member

I've added this to the agenda at the next TC39 meetup: tc39/agendas#248
The repository tracking the nullary coalescing proposal is available here: https://github.com/gisenberg/proposal-nullary-coalescing

sendilkumarn pushed a commit to sendilkumarn/proposal-optional-chaining that referenced this issue Jun 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants