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.
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: