Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 is the successor of #7996.
Add support for providing "catch all" variant constructors to tagged variants, to allow capturing all non-literal discriminator cases of a variant. This lets you write a variant that can enumerate a few known discriminators, and "catch" the rest at runtime in a single catch-all constructor, for those runtime values you haven't statically enumerated.
@catch(...)adds a pattern-only catch-all case for tagged variants:Example:
Rules:
@catch(...)is only valid on constructors with an inline record payload.@tag("...")name, or a field renamed to that runtime name with@as("...").@as("...")is only needed there when the source field name differs from the runtime discriminant name.@catch(int)->int@catch(float)->float@catch(string)->string@catch(...)requires an explicit type-level@tag("...").@catch(...)is not allowed on@unboxedvariants.intorfloat) and one string catch-all may appear in a variant.Only allowed in patterns
For this to be sound, we need to disallow constructing
@catchconstructors. This is rejected:If we don't reject this, you could do:
...and you'd end up with
One({thing: 1}), which is wrong type wise (thingisstringinOne). But, by disallowing expressions for just the constructors with@catch, you can't create that constructor (and with that can't do that unsound thing), but you can still create the other, well defined constructors, and you can still pattern match and so on. You can even do| Other(_) as o => osince that doesn't touch the constructor as an expression.