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

Wildcard patterns #1904

Merged
merged 3 commits into from
May 6, 2024
Merged

Wildcard patterns #1904

merged 3 commits into from
May 6, 2024

Conversation

yannham
Copy link
Member

@yannham yannham commented May 2, 2024

This PR adds a special pattern that match any value but doesn't bind anything, the wildcard pattern _.

This subsumes the previously special case of the catch-all case of match expression _ => default_value. Now a match expression is simply a list of patterns, and _ is just one of them.

This addition allows to use _ anywhere in a nested pattern, like 'Foo ('Bar _), which is especially useful when matching on enum variants. The ideas introduced for the typechecking of the catch-all case _ => of match expressions naturally extend to wildcards pattern: if somewhere in a nested pattern, there is a wildcard which is in the same position as an enum variant in a sibling pattern, like match { {foo = 'Bar} => something, {foo = _} => something_else}, this makes the corresponding enum type open: the above match expression has type forall r. {foo : [| 'Bar; r |]} -> SomeType.

A wildcard pattern doesn't force the value being matched on, following the idea that it's equivalent to let _x = value in body (but without binding _x). In general, from now on, we'll try to follow the idea that the semantics of destructuring let <pattern> = value in body should be equivalent to value |> match { <pattern> => body, _ => <NonExhaustiveDestructuringError> }. Following this guideline, it confirms that let _ shouldn't force the matched value. One last argument for this behavior is that fun _ => body is desugared to fun fresh_var => let _ = fresh_var in body, which means that making let _ strict would make fun _ => body strict in its argument, which is arguably very surprising (for reviewers, this contradicts what I said today earlier in the weekly meeting, because I made a mistake with wildcard patterns specifically - the "destructuring should force" is still valid for other patterns though, and it agrees with everything said here)

@yannham yannham requested review from jneem and vkleen May 2, 2024 18:19
@github-actions github-actions bot temporarily deployed to pull request May 2, 2024 18:21 Inactive
This commit adds a special pattern that doesn't bind anything and match
any value, the wildcard pattern `_`. This pattern doesn't force the
value being matched on.
@github-actions github-actions bot temporarily deployed to pull request May 3, 2024 09:22 Inactive
core/src/term/pattern/compile.rs Outdated Show resolved Hide resolved
core/src/typecheck/pattern.rs Outdated Show resolved Hide resolved
Co-authored-by: jneem <joeneeman@gmail.com>
@github-actions github-actions bot temporarily deployed to pull request May 4, 2024 13:54 Inactive
@yannham yannham enabled auto-merge May 4, 2024 14:45
@github-actions github-actions bot temporarily deployed to pull request May 4, 2024 14:47 Inactive
@yannham yannham requested a review from jneem May 4, 2024 15:15
@yannham yannham added this pull request to the merge queue May 6, 2024
Merged via the queue into master with commit e8e9a34 May 6, 2024
5 checks passed
@yannham yannham deleted the feat/wildcard-pattern branch May 6, 2024 04:18
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

Successfully merging this pull request may close these issues.

2 participants