Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, likematch { {foo = 'Bar} => something, {foo = _} => something_else}
, this makes the corresponding enum type open: the above match expression has typeforall 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 destructuringlet <pattern> = value in body
should be equivalent tovalue |> match { <pattern> => body, _ => <NonExhaustiveDestructuringError> }
. Following this guideline, it confirms thatlet _
shouldn't force the matched value. One last argument for this behavior is thatfun _ => body
is desugared tofun fresh_var => let _ = fresh_var in body
, which means that makinglet _
strict would makefun _ => 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)