Skip to content

Proposed simplification & increased symmetryΒ #322

@erights

Description

@erights

Playing a little fast and loose with the metagrammar, I think this proposal should be simplified to

A.1 Expressions

  • PrimaryExpr:

    • //... existing stuff
    • MatchExpr // Like in proposal
  • RelationalExpr: // unchanged, but useful below

    • //... existing stuff
    • RelationalExpr ( < | <= | >= | > ) ShiftExpr
    • RelationalExpr ( instanceof | in ) ShiftExpr
  • EqualityExpr:

    • //... existing stuff
    • EqualityExpr ( == | != | === | !== ) RelationalExpr
    • EqualityExpr ~= MatchPattern // like the proposed is
  • MatchPattern:

    • // Like in proposal and like destructuring
      • void // I'm a fan, and allows ...void
      • MatchPattern = AssignmentExpr // can we make defaults this simple?
      • [ (MatchPattern,)* (...MatchPattern)? ]
      • { (Prop: MatchPattern,)* (...MatchPattern)? }
      • Extractor
    • // Nice-to-have, unless Extractors can subsume these?
      • MatchPattern and MatchPattern // consider && ?
      • MatchPattern or MatchPattern // consider || ?
      • not MatchPattern // consider ! ?
      • if ( Expr )
    • // Different!
      • Identifier // Binding occurrence, like destructuring
      • ( < | <= | >= | > ) ShiftExpr
      • ( === | !== ) RelationalExpr
      • ~= MatchPattern
  • Left-Hand-Side Exprs: // enhance destructuring

    • //... existing stuff
    • void
    • Extractor // refutable
    • ~= MatchPattern // refutable
    • // How much more from MatchPattern could we add here?

The main change is how the distinction between comparing against values is expressed vs how bindings are introduced. The existing proposal treats a bare Identifier as a lexical reference to a variable from the enclosing scope, so it introduces bindings with a let, const, or var prefix. This is not at all like destructuring. IMO people will be confused by this asymmetry.

I saw in the proposal a suggestion of a pattern to be able to start with a (normally binary) relational operator followed by an expression. This is the opportunity to restore the symmetry with destructuring! This gives a way to introduce expressions whose value can be compared, including expressions using variables from outer scopes. Any Identifier appearing normally in a pattern is binding occurrence, just as one expects from destructuring.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions