Type Unions Syntax & Design Questions #8448
Unanswered
Panthr75
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
I do think that unions should be able to declare members. However, as struct unions would not be implemented as a type hierarchy I don't think it makes sense for the compiler to pretend that they do. It would complicate the feature both for the developer and for the compiler. public struct union Option<T> {
// cases
None,
Some(T value);
// members
public Option<TTo> Select<TTo>(Func<T, TTo> mapper) {
return this switch {
Some(var value) => Some(mapper(value)),
None => None
};
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've been mapping out syntax of a separate language I want to make based on C#, and I decided to start mapping out type union syntax, but I have ran into several important design and syntax questions that should be relevant to the Type Unions proposal.
Should Type Unions allow methods/properties/etc?
Due to how big of a question this is, I'll go into more detail plus a potential implementation solution.
Take for example the
Option
type union:It would be nice to define some helper methods that I generally see implemented in
Option
based libraries, such asMap
,FlatMap
, andGetValueOrDefault
.An idea that I have for implementing this is:
For example,
Option<TValue>
could look like this:This could then be lowered to:
If
Option<TValue>
was a regular/class union, it would be lowered to this:Based on the above implementation, it makes it seem like each type member of a Type Union is defined like a
record
. As such, Would it make sense to allow for Type Union Type Members to define custom constructors?And finally, Should Type Unions be allowed to implement interfaces?
I hope these questions can help forward the Type Union proposal by answering some important questions.
Beta Was this translation helpful? Give feedback.
All reactions