Skip to content

Unified pattern matching and destructuring syntax #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chriskuech opened this issue Apr 15, 2020 · 8 comments
Closed

Unified pattern matching and destructuring syntax #160

chriskuech opened this issue Apr 15, 2020 · 8 comments

Comments

@chriskuech
Copy link

One of the issues I've observed across various drafts is the confusing overlap between destructuring and pattern matching. A primary difference between destructuring and matching is that destructuring will match a missing value as undefined whereas pattern matching should fail.

Inspired by TypeScript's non-null assertion operator, I propose introducing a post-fix unary operator ! in matches that binds the preceding name only if the destructured value is truthy.

const val = {a: 1};

const {a, b} = val; // a => 1, b => undefined
const {a, b!} = value; // throws destructuring error

This would enable syntax for conditional if let expressions.

if (const {err!} = res) {
    throw err;
}

This same syntax can be used with pattern matching:

const res = case (res) {
    {err!} -> err,
    {statusCode: 200} -> ok,
    {statusCode!} -> mapStatusCode(statusCode),
}
@erights
Copy link

erights commented Apr 15, 2020

I like the idea. Does the syntax work out, given all the ASI constraints, etc?

How would TypeScript itself adopt it? Doesn't it conflict with TypeScript's own use of !?

(This TypeScript conflict killed infix bang as sugar for the eventual-send proposal.)

@chriskuech
Copy link
Author

To my knowledge, TypeScript never allows ! on the left side of a variable assignment, so this should never cause issues.

@ljharb
Copy link
Member

ljharb commented Apr 15, 2020

This would need to be its own proposal, at which point pattern matching could adopt it - while the potential difference between [[HasOwnProperty]] and undefined is something to consider, I don't think this proposal needs to be blocked on this syntax suggestion.

@chriskuech
Copy link
Author

@ljharb , this could not be in its own proposal. The whole point is to unify syntax to ease learning and maximize likelihood of acceptance. Separating this proposal into a separate proposal would ensure that the pattern matching syntax would not have compatibility with destructuring syntax because of the undefined incompatibility.

@hax
Copy link
Member

hax commented Apr 16, 2020

I like the idea, but:

  1. If I understand correctly, now {err} -> err will always match if input is any non-null value, which make such syntax not very useful by default.
  2. ! as TS non-null assertion is for both undefined and null, so for consistency, {err!} -> err should also fail if err is null.
  3. The difference between pattern matching and destructuring is not only undefined, another main diff is [a, b] -> ... only match when the input have exactly two item.

@hax hax mentioned this issue Apr 16, 2020
@ljharb
Copy link
Member

ljharb commented Apr 17, 2020

@chriskuech a syntax like that absolutely must be its own proposal, because it wouldn't just apply inside pattern matching - it'd have to work everywhere.

@stiff

This comment has been minimized.

@ljharb
Copy link
Member

ljharb commented Apr 21, 2021

The proposal has been updated in #174. If you have any questions about the updated syntax, please file a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants