Skip to content
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

Add pattern matching support for records with @optional fields. #5452

Closed
cristianoc opened this issue Jun 20, 2022 · 4 comments
Closed

Add pattern matching support for records with @optional fields. #5452

cristianoc opened this issue Jun 20, 2022 · 4 comments

Comments

@cristianoc
Copy link
Collaborator

cristianoc commented Jun 20, 2022

This is follows on from #5423.

Example:

type t = {x: int, @optional y: string}

let v = {x: 3, y: "ab"}

let q = switch v {
| {x: 3, y: Some("ab")} => true
| _ => false
}

There's asymmetry between expressions and patterns.

Proposal: adopt for patterns the same convention used for expressions.

The example above now becomes:

let q = switch v {
| {x: 3, y: "ab"} => true
| _ => false
}

And for catch-all:

  • Q: what would | {y: _} => mean? A: it means Some(_).
  • Q: what does | {y : @optional _ } mean? A: it means _.
cristianoc added a commit that referenced this issue Jun 20, 2022
This gives pattern matching that mimics expression constructor, where the variable by default is not optional.
The generated code seems fine, thought the checks for exhaustiveness are not kicking in.

See #5452
@rpominov
Copy link

What will be an alternative to | {x: 3, y: None} => true in the proposed syntax?

@cristianoc
Copy link
Collaborator Author

What will be an alternative to | {x: 3, y: None} => true in the proposed syntax?

Same as with expressions.
Btw this is not a syntax. That needs to be added on top.

@cristianoc
Copy link
Collaborator Author

The expression, which was like this before this addition of patterns, looks like this:

{x: 3, y: @option None}

cristianoc added a commit that referenced this issue Jun 22, 2022
This gives pattern matching that mimics expression constructor, where the variable by default is not optional.
The generated code seems fine, thought the checks for exhaustiveness are not kicking in.

See #5452
cristianoc added a commit that referenced this issue Jun 22, 2022
This gives pattern matching that mimics expression constructor, where the variable by default is not optional.
The generated code seems fine, thought the checks for exhaustiveness are not kicking in.

See #5452
@cristianoc cristianoc changed the title Investigate expressions vs patterns in @optional records. Add pattern matching support for @optional records. Jun 22, 2022
@cristianoc cristianoc changed the title Add pattern matching support for @optional records. Add pattern matching support for records with @optional fields. Jun 22, 2022
cristianoc added a commit that referenced this issue Jun 22, 2022
This gives pattern matching that mimics expression constructor, where the variable by default is not optional.
The generated code seems fine, thought the checks for exhaustiveness are not kicking in.

See #5452
@cristianoc
Copy link
Collaborator Author

In master.

mununki pushed a commit to mununki/rescript-compiler that referenced this issue Jul 15, 2022
This gives pattern matching that mimics expression constructor, where the variable by default is not optional.
The generated code seems fine, thought the checks for exhaustiveness are not kicking in.

See rescript-lang#5452
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants