Why You Need Discriminated Unions in C#

preview_player
Показать описание
#csharp #dotnet #programming
Рекомендации по теме
Комментарии
Автор

OneOf can get really messy when it comes to async and/or generics. I love the idea, but the workarounds to get it to work with C# put me off

TodPalin
Автор

My experience with discriminated unions in C# have not been good at all, specially when you start working with async code and type augmentation, its impossible to maintain it.
I also believe the first example is the better approach, exceptions aren't evil and you should use them to fail fast and have cleaner, more concise contracts. I would only add that instead of returning Nullable<T> and throwing on validation, you should clean up the contract and return T, otherwise throw (if you can't fulfill the contract), this avoids garbage "if not null" statements; you only get what you asked for!

dsein
Автор

oOoOO yes yes yes this is what I was looking for !!!! The Result<> and Option<>

max_ishere
Автор

I really really wish C# had this. Monads would make me very happy.

iuribrindeiro
Автор

I generally love this idea but it feels like you are just fighting C# when you do this. Languages that support this from their core design like Go are way smoother with this.

TheFeljoy
Автор

So a Result of an Option of a Movie?
Sorry, I'm a little rusty rn

andrecarneiro
Автор

This type of control flow is especially good in minimal APIs with the TypedResults responses. Easy and reliable Swagger documentation at the same cost.

markusn
Автор

Great idea. If only there was a programming language built entirely on this from the ground up ;)

yondaime
Автор

Exceptions are for exceptional conditions. Passing in an invalid Movie object seems unexpected and exceptional, here. I understand exceptions are expensive .. so yes, user input (request fields) should be validated as far upstream as possible.
But this doesn't seem like a problem that's worth complexifying the entire programming model, to solve.

arithex
Автор

waiting for this feature, its been ages

Endomorphism
Автор

M$ just updated the meetings document on gh talking about DUs. They are working on it. Or at least thinking about working on it.

iuribrindeiro
Автор

I think this way return type becomes too convoluted.
This should be solved through good old checked error validation. Movie class can have HasValue property and potentially carry exceptions.
There is no need to re-invent things for which we have good programming practices.

SimpMcSimpy
Автор

I’ve used this and it’s ok. I like that you handle the logic of converting it into an http status in the web layer. I’ve seen people create response objects which map the whole thing to http and I hate that. It’s just interesting how we’ve gone from return errors to exceptions and now another type of return. World goes round and round.

minnesotasteve
Автор

Why the consumer needs to know if it throws an exception ? It expects a movie or null. Anything else the consumer should not deal with it. It is valid to throw an exceptiom here.
If the consumer needs to about the case that throws an exception a new methof should be created: can...() Isn't this a better approach ?

elieanimations
Автор

In typescript this would be like an OR of algebraic data types:
type MyCustomType = TypeA || TypeB || TypeC;

AlejandroMamani
Автор

This is why I have always felt difficulty to understand why people hate Java's checked exceptions so much! At least they are part of the method contract, even though it wasn't perfect or beautiful. But just like DUs they are required to handle by the caller and this is validated by the compiler.
I love C# and have been writing it for almost 20 years, but actually I learned Java even earlier and I have always felt that C# had missed something very important here, even though I rarely touch Java these days.

jongeduard
Автор

The problem I have with this approach, and why don't like it - it is because in 99% of the software you DO NOT need to react specific way to some problem, instead you probably just show an error. Exceptions are great, mainly because of 2 features: you can't ignore them, and they go up in the call stack, no matter how big it is. With DU approach you code basically became go-style - where 80% of you code - is redurant error checking with simple returning the error on all call stack levels, neglecting the point of doing this in the first place. Most of the software just don't get any value from this

ilushkins
Автор

I use an Either type. Not sure how it's done in C# but I'm interested in learning it.

skypuff
Автор

how does OneOf work?
is it a reference type or a struct?
Does it store a reference or the value itself?
so if i do OneOf<int, long> would it store a reference to them or would it be a struct with the size of the biggest type here which is long 64bit

i can't find the docs for it for some reason

normally how i would do unions is, if i were to make some lib for it
i would make a struct with and place all of the things it can return in it and offset everything to 0 so they share the same memory
so struct is the size of the biggest element
BUT i would also add a byte enum at the beginning of the struct that will store the type of the current value

so if i were to do a union of `long` and `int` with that method, the size of the struct would be 8(size of biggest type `long`) + 1(enum keeping track of the type of the current value) bytes

nomadshiba
Автор

Sounds like Rust enums with extra steps

professornumbskull