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

Syntax: Use ?& (or anything else that correct the inconsistency between a?.b and a?.[b]) #34

Description

@claudepache

After careful consideration (see in particular #5), I think that the best syntax satisfying the difficult constraints we face (strong BC, consistency) is the following:

a ?& . b    // instead of   a ?. b
a ?& [ i ]  // instead of   a ?. [ i ]
a ?& ( x )  // instead of   a ?. ( x )

Pros

  • Complete consistency between the three syntactical forms.
  • Having a ?& token distinct from . is pedagogically better: One can picture the ?& token checking whether the LHS is null/undefined and performing short-circuiting if applicable, then the subsequent ., [ or ( token performing regular property access or function invocation.
  • Zero conflict/ambiguity in syntax with other features, zero parsing issue.

Cons

  • Somewhat more awkward to type.
  • Different from other languages. (However, it is reminiscent of both ?. in C#, Swift, CoffeeScript and Groovy, and &. in Ruby.)

About the choice of ?&

  • The ? recalls that we are dealing with null/undefined, as this character is used in several languages for denoting an optional/nullable value or object.
  • The & recalls that the short-circuiting condition resemble the one of the && operator (replacing “falsy” with “nullish”).
  • The order between those two chars is carefully designed: ?: “check for null”; &: “perform eventual short-circuiting”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    alternative syntaxpast ideas and discussions about alternative syntaxes

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions