Allow automatic sub-typing from variants of another data-type #891
KtorZ
started this conversation in
Core language features
Replies: 1 comment 4 replies
-
Similarly, would it be possible to explicitly declare constructor indices?
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The idea is to provide a way to create types that have constructor indexes at specific height, instead of being numbered from 0 onward. This is useful in combination with #889 as it can reduce the cost of casting a value into a type if a branch is only interested in a sub-part of the type.
For example, imagine a type:
When checking if something is of type
Foo
, we possibly have to check for constructor 0, 1 and 2 as well as any fields they may carry. Yet, we might only be interested in a specific variant (say,B
). So, we could instrument the type-checker to only require a cast toFoo
restricted toB
. Fundamentally,B
would be a record of a single constructor but with its constructor index equal to 1 and not to 0.A possible syntax for that could be to piggyback on type-alias as such:
Which would be equivalent to defining:
Beta Was this translation helpful? Give feedback.
All reactions