1 of 11

Optional Chaining Operator

(formerly Null Propagation Operator)

Spec Author: Claude Pache

Champion: Gabriel Isenberg

2 of 11

Goals

  • Recap
  • Speak to open questions and changes since stage 0 to stage 1
  • Stage 2

3 of 11

Recap: Syntax

obj?.prop // optional property access�obj?.[expr] // optional property access�func?.(...args) // optional function or method call

Syntax:

Semantics:

a?.b // undefined if a is null/undefined, a.b otherwise�a?.[++x] // If a evaluates to null/undefined, the variable x is *not* incremented.�a?.b.c().d // undefined if a is null/undefined// throws if b or c is null/undefined// throws if c() returns null/undefined��a?.() // a is invoked if not null/undefined

4 of 11

Recap: Questions from stage 1 meeting

  • Should we return undefined, null, or null and undefined?
  • Should we have a better default operator than || ?
    • Complementary proposal: existential operator
  • How should computed property access and function calls be handled?
  • What is the scope of short circuiting and free grouping?
  • Is there syntax for conditional function invocation, or only property lookup?

5 of 11

Should we return undefined, null, or null and undefined?

  • Returns undefined if property is absent.
  • Returns null if property is defined with a null value.�
  • a?.b
    • Not intended to preserve information about the base object a
    • Intended to give information about the property b
    • If property b is absent, it is undefined
    • If property b is null, it is null.
  • (null)?.b // undefined, no error
  • (undefined)?.b // undefined, no error

More discussion: Write justification for choices in syntax and semantics

6 of 11

How should computed property access be handled?

  • No errors swallowed.
  • A property getter that throws an error will still throw an error.

7 of 11

Short circuiting and free grouping

  • Option 1 (consistent): “the current property access, method or function call”
    • a?.b()?.c?.d // no short circuiting, property access is safe
  • Option 2: “the current chain of property accesses, method and function calls”
    • a?.b().c.d // with short circuiting, property access is safe if a is null or undefined.
  • Option 3: Includes “Left-Hand-Side Expression”, which includes new and tagged templates
  • Option 4 (current): Parens used for grouping should not stop short-circuit
    • (new a?.b.c).d[x] `{y}`
  • From Claude (in favor of option 2):
    • A refactoring of the specification is in underway, where short-circuiting will be explained more like `||` or `&&`. With the new version, Option 3 will be void, and parens will naturally stop propagation.
    • Parens will stop short-circuiting, because it will be explained on a purely syntactical basis instead of a propagation of a Reference.
      • (a?.b()).c.d // property access throws if a?.b() returns null or undefined
    • More natural spec

8 of 11

Is there only syntax for property lookup?

  • The following forms are allowed:
    • a?.b
    • a?.[b]
    • a?.(b)
    • delete a?.b // no-op if a is null or undefined.
  • Should new a?.(b), a?.`{b}` be allowed?
    • new and template literals not to be supported unless there is a good use case.
  • From an analysis of 500kloc of CoffeeScript (@alangpierce, Decaffeinate project)
    • Projects included Atom, ShareLaTeX, CodeCombat, SwitchyOmega, Trix, Vimium and more)
    • new was never used, delete was used once
    • assignments (a?.b = c) had some usage.
    • Short circuiting wasn’t used in 67% of use cases.. If short-circuiting only applied to direct method syntax (a?.b()), 93% of all use cases would be covered.
    • alangpierce/coffeescript-soak-stats

9 of 11

Open questions and issues

  • “Is there any discussion on a similar mechanism for destructuring?”
    • let { x, ?y, ?z: { a: r }} = undefined; // x throws, y and z do not
    • let ?{ x, y, z } = null; // nothing throws
  • Ongoing refactoring that will eliminate the need of the Nil reference artifact
  • Should delete/new be allowed?
    • Claude: Delete should be allowed, new should be unsupported barring good reason.
  • Should assignments be allowed?
    • Claude: Yes, provided the semantics are sufficiently clear.

10 of 11

Status

  • Seeking stage 2

11 of 11

Q&A