-
Notifications
You must be signed in to change notification settings - Fork 449
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
Variant type spreads #6316
Variant type spreads #6316
Conversation
…s runtime configuration, and get some basic error reporting going
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.
As discusses offline, there might be opportunities to analyse the implementation further and perhaps simplify it.
However, for an early feature, this has plenty of tests and there's little risk that this will affect other language features.
The implementation can be refined later when there's more time.
Feature
Allow spreading variant type definitions.
Assumptions
@unboxed
and@tag
configuration needs to match between the variants. If they don't match, you'll get an error.aa
are taken and copied tobb
.Description of how this PR works:
Parsing
Each spread parses to a constructor called "...", with a single payload pointing to the variant the spread is for. So this:
Parses to this:
These "spread" constructors will then be handled in the type checker.
Handling the spreads in the type checker
To process the spread elements in the type checker, we introduce a temporary "dummy" constructor, holding each variant type we're spreading, in the parse tree before it enters the type checker. In addition to this, we also copy paste each constructor from the variant we're spreading into the parse tree of the variant being spread into.
These dummy constructors, e.g.,
...(b)
, are maintained temporarily during the type checking process, to be later removed.To illustrate:
The parse AST when this hits the type checker will look like this:
Subsequently in the type checker, we iterate through every constructor and if it turns out to be a dummy constructor, we look up the variant that dummy constructor holds (e.g.,
...(b)
, lookup variantb
). We then replace the constructor with the types provided by the type checker in the dummy constructor.After this process, we purge all dummy constructors, leaving behind a fully defined variant type with each inlined constructor, complete with its correct type and arguments, derived from each spread variant.
This approach allows us to leverage the type checker to perform most of the complex work.
Important Considerations: