-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Feature request: Alternative{T,U}
#17073
Comments
I'd love to see something like this. I would have assumed the representation would be more like the following:
Why store a |
Yes; similar to a C |
We could inline unions this way but it will still require an inline tag so that doing I guess you could do something like (assuming we do inline unions) immutable TaggedUnionElt{Tag,T}
value::T
end
immutable Alternative{T,U}
value :: Union{TaggedUnionElt{0,T},TaggedUnionElt{1,U}}
end not sure how much of a good idea that is though. |
(it does require a pointer which is larger than a byte but the ABI will probably have you pad it this way anyway) |
This reminds me of the "lazy tag" idea: instead of storing object references as pointers to tagged values, keep the tag to the reference. |
An efficient implementation of a type
Alternative{T,U}
in the sense of a discriminated union requires support from the compiler, hence I'm opening a feature request here for feedback.I imagine a type
Alternative{T,U,...}
that holds a value of any of the typesT
,U
, etc., and remembers which alternative is valid. This is an extension of both enums andNullable
: Enums correspond to anAlternative
where all types areVoid
, andNullable{T}
is identical toAlternative{T,Void}
. Similar to the way in whichNullable{Void}
still remembers whether it holdsVoid
element or not,Alternative{T,T}
still remembers which of the two alternatives is valid, different fromUnion{T,T}
.Alternative
could be implemented asbut obviously a more efficient representation would be valuable so that bitstypes don't require boxing.
If there is future work on the compiler to make
Nullable
more efficient, then maybe this work can keep efficiency forAlternative
in mind.The text was updated successfully, but these errors were encountered: