by tablloyd · 3y ago · 0 comments
Eg: https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgP...
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals...
Probably one of my favorite recent-ish additions to the language.
For now, you can get a reasonable DU via an [external library](https://github.com/mcintyre321/OneOf).
[Nick Chapsas Video on Usage](https://www.youtube.com/watch?v=7z-xjijYfcI).
https://github.com/linkdotnet/Blog.Discussions/discussions/7...
Is is kind of a pain particularly when working across Typescript projects. OneOf is cool, but it DOES NOT work well with null and thus optional parameters.
(There are multiple proposals tracking the idea. This seems the most comprehensive and "central": https://github.com/dotnet/csharplang/issues/7016)
I think they would add it by C# 14 or 15.
I believe I am overlooking something (probably obvious even to me) since i know:
https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref...
is perhaps a misuse of the term in this context.
(I'm guessing you mean more like in Rust, but am not sure.)
Anyone that cares so deeply about them can do the work on a F# assembly.
[1] https://devblogs.microsoft.com/dotnet/new-csharp-12-preview-... [2] https://devblogs.microsoft.com/dotnet/new-csharp-12-preview-...
But in any case I really love this addition to the language but the inability to have multi-line or block expression arms is a constant annoyance for me.
You can even combine these with the new one line record syntax to create a poor man’s discriminated union.
The workaround for that is the same as the workaround for the really lame one line limitation: you need to call a (preferably (static) local) function in the handler portion and then return something like `true` assigned to a discard. Hacks all around!
Eg
_ = foo switch a when … => CaseA(foo), _ => CaseB(foo);
With CaseA and CaseB returning bool in order to call a function depending on the value of foo rather than assign a value.
F# / C# interop ... cool thing, just to clarify.