From 6db2c6cfa202c83f7dfba7da31a3c190010016b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Tue, 21 Apr 2026 20:00:35 +0800 Subject: [PATCH] =?UTF-8?q?ref:=20=E6=B7=BB=E5=8A=A0=20OneOf=EF=BC=8C?= =?UTF-8?q?=E9=87=8D=E6=9E=84=20ArgumentReplace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- General/OneOf.cs | 47 ++++++++++++++++++++++++++++++++++++++++++++++ MeloongCore.csproj | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 General/OneOf.cs diff --git a/General/OneOf.cs b/General/OneOf.cs new file mode 100644 index 0000000..3a2950e --- /dev/null +++ b/General/OneOf.cs @@ -0,0 +1,47 @@ +using System; + +namespace MeloongCore; + +public readonly struct OneOf { + private readonly int _index; + private readonly T0 _value0; + private readonly T1 _value1; + public static implicit operator OneOf(T0 value) => new(0, value, default); + public static implicit operator OneOf(T1 value) => new(1, default, value); + private OneOf(int index, T0 value0, T1 value1) { + _index = index; + _value0 = value0; + _value1 = value1; + } + + public TResult Match(Func f0, Func f1) => + _index switch {0 => f0(_value0), _ => f1(_value1)}; + public void Switch(Action f0, Action f1) { + if (_index == 0) f0(_value0); + else if (_index == 1) f1(_value1); + } +} + +public readonly struct OneOf { + private readonly int _index; + private readonly T0 _value0; + private readonly T1 _value1; + private readonly T2 _value2; + public static implicit operator OneOf(T0 value) => new(0, value, default, default); + public static implicit operator OneOf(T1 value) => new(1, default, value, default); + public static implicit operator OneOf(T2 value) => new(2, default, default, value); + private OneOf(int index, T0 value0, T1 value1, T2 value2) { + _index = index; + _value0 = value0; + _value1 = value1; + _value2 = value2; + } + + public TResult Match(Func f0, Func f1, Func f2) => + _index switch {0 => f0(_value0), 1 => f1(_value1), _ => f2(_value2)}; + public void Switch(Action f0, Action f1, Action f2) { + if (_index == 0) f0(_value0); + else if (_index == 1) f1(_value1); + else if (_index == 2) f2(_value2); + } +} diff --git a/MeloongCore.csproj b/MeloongCore.csproj index 1afb50e..872def8 100644 --- a/MeloongCore.csproj +++ b/MeloongCore.csproj @@ -9,7 +9,7 @@ false Debug;Release;Snapshot true - 12.0 + 13.0