易于使用的 F# 类型的 ~ 歧化 ~ 结构体,可用于 C# 中,具有详尽的编译时匹配
"Ah! It's like a compile time checked switch statement!" - Mike Giorgaras
install-package OneOf
This library provides F# style discriminated unions for C#, using a custom type OneOf<T0, ... Tn>. An instance of this type holds a single value, which is one of the types in its generic argument list.
I can't encourage you enough to give it a try! Due to exhaustive matching DUs provide an alternative to polymorphism when you want to have a method with guaranteed behaviour-per-type (i.e. adding an abstract method on a base type, and then implementing that method in each type). It's a really powerful tool, ask any f#/Scala dev! :)
PS If you like OneOf, you might want to check out ValueOf, for one-line Value Object Type definitions.
The most frequent use case is as a return value, when you need to return different results from a method. Here's how you might use it in an MVC controller action:
…
It's simple to use OneOf as an Option type - just declare a OneOf<Something, None>. OneOf comes with a variety of useful Types in the OneOf.Types namespace, including Yes, No, Maybe, Unknown, True, False, All, Some, and None.
IActionResult, or even worse, a non-descriptive type (e.g. object)You can use also use OneOf as a parameter type, allowing a caller to pass different types without requiring additional overloads. This might not seem that useful for a single parameter, but if you have multiple parameters, the number of overloads required increases rapidly.
public void SetBackground(OneOf<string, ColorName, Color> backgroundColor) { ... }
//The method above can be called with either a string, a ColorName enum value or a Color instance.
You use the TOut Match(Func<T0, TOut> f0, ... Func<Tn,TOut> fn) method to get a value out. Note how the number of handlers matches the number of generic arguments.
switch or if or exception based control flow:This has a major advantage over a switch statement, as it
requires every parameter to be handled
No fallback - if you add another generic parameter, you HAVE to update all the calling code to handle your changes.
In brown-field code-bases this is incredibly useful, as the default handler is often a runtime throw NotImplementedException, or behaviour that wouldn't suit the new result type.
E.g.
OneOf<string, ColorName, Color> backgroundColor = ...;
Color c = backgroundColor.Match(
str => CssHelper.GetColorFromString(str),
name => new Color(name),
col => col
);
_window.BackgroundColor = c;
There is also a .Switch method, for when you aren't returning a value:
OneOf<string, DateTime> dateValue = ...;
dateValue.Switch(
str => AddEntry(DateTime.Parse(str), foo),
int => AddEntry(int, foo)
);
As an alternative to .Switch or .Match you can use the .TryPick methods.
//TryPick methods for OneOf<T0, T1, T2>
public bool TryPickT0(out T0 value, out OneOf<T1, T2> remainder) { ... }
public bool TryPickT1(out T1 value, out OneOf<T0, T2> remainder) { ... }
public bool TryPickT2(out T2 value, out OneOf<T0, T1> remainder) { ... }
The return value indicates if the OneOf contains a T or not. If so, then value will be set to the inner value from the OneOf. If not, then the remainder will be a OneOf of the remaining generic types. You can use them like this:
IActionResult Get(string id)
{
OneOf<Thing, NotFound, Error> thingOrNotFoundOrError = GetThingFromDb(string id);
if (thingOrNotFoundOrError.TryPickT1(out NotFound notFound, out var thingOrError)) //thingOrError is a OneOf<Thing, Error>
return StatusCode(404);
if (thingOrError.TryPickT1(out var error, out var thing)) //note that thing is a Thing rather than a OneOf<Thing>
{
_logger.LogError(error.Message);
return StatusCode(500);
}
return Ok(thing);
}
You can declare a OneOf as a type, either for reuse of the type, or to provide additional members, by inheriting from OneOfBase. The derived class will inherit the .Match, .Switch, and .TryPick methods.
…
You can automatically generate OneOfBase hierarchies using GenerateOneOfAttribute and partial class that extends OneOfBase using
a Source Generator (thanks to @romfir for the contribution :D). Install it via
Install-Package OneOf.SourceGenerator
and then define a stub like so:
[GenerateOneOf]
public partial class StringOrNumber : OneOfBase<string, int> { }
During compilation the source generator will produce a class implementing the OneOfBase boiler plate code for you. e.g.
public partial class StringOrNumber
{
public StringOrNumber(OneOf.OneOf<System.String, System.Int32> _) : base(_) { }
public static implicit operator StringOrNumber(System.String _) => new StringOrNumber(_);
public static explicit operator System.String(StringOrNumber _) => _.AsT0;
public static implicit operator StringOrNumber(System.Int32 _) => new StringOrNumber(_);
public static explicit operator System.Int32(StringOrNumber _) => _.AsT1;
}
暂无开放 Issues,或尚未同步最近议题。