What does HackerNews think of OneOf?

Easy to use F#-like ~discriminated~ unions for C# with exhaustive compile time matching

Language: C#

#3 in C#
Until that feature lands there's this https://github.com/mcintyre321/OneOf which is pretty nice.
DU is available in C# via packages like OneOf and Dunet.

Dunet is really good.

https://github.com/mcintyre321/OneOf

https://github.com/domn1995/dunet

It's on the roadmap and will arrive at some point:

https://github.com/dotnet/csharplang/blob/main/proposals/dis...

Agree.

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).

Apologies, but here's the obligatory link to my OneOf exhaustive type matching library: https://github.com/mcintyre321/OneOf
I maintain a library for doing exactly this in C# (https://github.com/mcintyre321/OneOf). I tried to replicate it in Python as I'm writing it at the moment, but couldn't quite manage it. Can you have a multi generic typed Union in python?
You can get a pretty reasonable facsimile using some libraries I made:

Discriminated unions: https://github.com/mcintyre321/OneOf and One line value objects: https://github.com/mcintyre321/ValueOf

... and exhaustive matching: https://github.com/mcintyre321/OneOf

I'm not saying it's as good as writing F#, or that C# shouldn't include these features of course.

If you're after Option or ADTs (Unions) for C#, I can heartily recommend https://github.com/mcintyre321/OneOf , it's brilliant.

But then I would say that ;)

There's also the OneOf NuGet package [1] which I've put to good use in a number of projects. Using `object` and `is` means you can't enforce what types get passed in, nor can you ensure that all possible types actually get handled.

[1]: https://github.com/mcintyre321/OneOf/

I retrofitted F# style "discriminated unions" (which are basically sealed heirarchies) into C# by creating a series of generic types `OneOf` which can hold exactly one value,

Each type has a `.Match` and `.Switch` methods, in to which you have to pass lambdas to handle each case `.Match(Func`.

I don't know if this would work in Java, given the generic type erasure, but it might...

1. https://github.com/mcintyre321/OneOf

For anyone who is disappointed at the lack of exhaustive matching / Discriminated Unions, please try out my library OneOf* (https://github.com/mcintyre321/OneOf). Hopefully something similar will be brought into C# soon, but in the meantime, these libraries provide fairly idiomatic substitutes.

Also, ValueUtils (https://www.nuget.org/packages/ValueUtils/) is nice for making value types.

*or one of the many alternatives (https://www.nuget.org/packages?q=discriminated+unions) e.g. SuccincT, DiscU

You can achieve something similar via a library: https://github.com/mcintyre321/OneOf

I also think language level support for this would be a killer feature, it turns OOP and the requirement to use polymorphism to guarantee strategy-per-type on it's head.