-
Notifications
You must be signed in to change notification settings - Fork 90
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
ADT destructuring #1812
ADT destructuring #1812
Conversation
|
||
Pattern { data: PatternData::Any(<>), alias: None, span } | ||
}, | ||
#[inline] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for some reason we need the inline pragma to get LALRPOP to accept the grammar as non-ambiguous (it was suggested by LALRPOP error message itself).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're introducing a lot of differences between enum tags and aenum variants, and it makes me wonder if we shouldn't either allow something like [| 'Foo,, 'Foo Number |]
or really go with the unit type route were discussing. But I think that's better addressed in another iteration on the idea.
b744381
to
e024740
Compare
🎉 All dependencies have been resolved ! |
This is actually a very good point. We can go both routes (make them different, or make |
Add a primitive operation to extract the argument from an enum variant, which will prove useful to enum matching and destructuring.
This commit adds support for enum variant patterns in destructuring forms (let bindings and function declarations). This paves the way for matching on ADTs once we allow patterns not only appear in a destructuring binding, but also in a match expression.
A test revelead that the typechecking errors were off sometimes for destructuring lets: if the destructured value didn't correspond to the pattern, the type elaborated from the pattern appeared as the inferred type of the expression, and the inferred type of the expression as the expected type (i.e. the two were reversed). This causes incorrect error messages like "extra row" instead of "missing row". This commits fix the issue by unifying the type deduced from the pattern and the type inferred from the bound expression in the right order (unification is symmetric, as far as typechecking is concerned, but for error reporting we do distinguish between expected and inferred type).
68ff4dc
to
b192b0a
Compare
Depends on #1799
This PR adds support for ADTs (enum variants) in patterns. Patterns can currently only be used in destructuring, which isn't very useful for ADTs: you have to know the tag in advance to be able to actually destructure them. However, it's a first way to inspect enum variants, and a stepping stone toward full pattern matching as we will use the same notion (and the same Rust type in practice) for patterns in a match expression, and common implementations for type elaboration as well as the extraction code.
Content
%enum_unwrap_variant%
,%enum_is_variant%
,%enum_get_tag%
) as well as a new internal contract$enum_variant
Follow-ups
Unit
type and the()
value)