✅ Is it possible to use union type in C#?
I'm not sure that it's called exactly Union Type
Let me introduce the issue
I have this class which represent generator:
I want somehow add following logic:
I don't want to override method because it will be too much code in one class
dictionary x2, Generate x2, InternalGenerator x2
9 Replies
Enumerable.Union Method (System.Linq)
Produces the set union of two sequences.
I think it does not answer my question
I mean I need smth like Either maybe
language-ext has some things like this if you prefer FP:
https://github.com/louthy/language-ext
The language doesn't have it by default (yet). What you're looking for is a discriminated union, right?
GitHub
GitHub - louthy/language-ext: C# functional language extensions - a...
C# functional language extensions - a base class library for functional programming - GitHub - louthy/language-ext: C# functional language extensions - a base class library for functional programming
Or tagged union, depending on which FP circles you identify with.
There's a million either/option/DU libraries out there. LanguageExt is cool although a bit intrusive to the average C# workflow. There's also OneOf which is a pretty solid either implementation. You could also make your own, they're not too difficult to implement, and with a few helper methods it's pretty workable. or if you fancy records and better pattern matching capabilities
thanks, guys
that is what I needed
I like how concise the abstract record is. Can you show an example of usage?
with a couple helper/extension methods and implicit conversions
This has a lot of room for improvement but imo it's the nicest you'll probably get
Unfortunately C#'s type inference isn't powerful enough to make this like... actually nice. You run into a lot of annoyances really quickly when you try doing something like this.
If you look at for instance Rust then you'll find language features (besides just plain DU support) which make things like this really good.
looks good, thanks for explanation
I will try to use this