From e0ccd985d3c443f357d3c777980b0465ec065813 Mon Sep 17 00:00:00 2001 From: Jiri Pokorny Date: Thu, 14 Sep 2023 16:45:03 +0200 Subject: [PATCH 1/2] Added prototype of allocation free Match method --- .../Object/IfMatchBenchmarks.cs | 46 +++++++++++++++++++ .../Extensions/ObjectExtensions_ValueMatch.cs | 18 ++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/FuncSharp.Benchmarks/Object/IfMatchBenchmarks.cs diff --git a/src/FuncSharp.Benchmarks/Object/IfMatchBenchmarks.cs b/src/FuncSharp.Benchmarks/Object/IfMatchBenchmarks.cs new file mode 100644 index 0000000..49f0ed1 --- /dev/null +++ b/src/FuncSharp.Benchmarks/Object/IfMatchBenchmarks.cs @@ -0,0 +1,46 @@ +using BenchmarkDotNet.Attributes; + +namespace FuncSharp.Benchmarks; + +[MemoryDiagnoser] +public class IfMatchBenchmarks +{ + // Last Result - 14.9.2023 - 5.959 ns - 48 B + [Benchmark] + public void MapTrue() + { + var result = this.Map(true); + } + + // Last Result - 14.9.2023 - 0.0046 ns - 0 B + [Benchmark] + public void MapValueTrue() + { + var result = this.MapValue(true); + } + + // Last Result - 14.9.2023 - 0.0012 ns - 0 B + [Benchmark] + public void IfTrue() + { + var result = this.IF(true); + } + + public int Map(bool source) + { + return source.Match(true, a => 5, a => 11); + } + + public int MapValue(bool source) + { + return source.Match(true, 5, 11); + } + + public int IF(bool source) + { + if (source) + return 5; + + return 11; + } +} \ No newline at end of file diff --git a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs index c63a089..be4919e 100644 --- a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs +++ b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs @@ -8,6 +8,24 @@ namespace FuncSharp { public static partial class ObjectExtensions { + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + + return otherwise; + } + /// /// Matches the value with the specified parameters and returns result of the corresponding function. /// From e812d823dbfb532f11f6db81732497ca64b125fa Mon Sep 17 00:00:00 2001 From: Jiri Pokorny Date: Tue, 26 Sep 2023 22:00:06 +0200 Subject: [PATCH 2/2] Implemented allocation free match method in template --- .../Extensions/ObjectExtensions_ValueMatch.cs | 1292 ++++++++++++++++- .../Extensions/ObjectExtensions_ValueMatch.tt | 19 + 2 files changed, 1310 insertions(+), 1 deletion(-) diff --git a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs index b686c3c..334fa1a 100644 --- a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs +++ b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.cs @@ -28,6 +28,23 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 1 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -110,6 +127,28 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 2 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -214,6 +253,33 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 3 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -340,6 +406,38 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 4 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -488,6 +586,43 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 5 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -658,6 +793,48 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 6 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -850,6 +1027,53 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 7 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -1064,6 +1288,58 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 8 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -1300,6 +1576,63 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 9 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -1558,6 +1891,68 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 10 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -1838,6 +2233,73 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 11 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -2140,6 +2602,78 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 12 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -2464,8 +2998,85 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 13 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// [Pure] - public static async Task MatchAsync( + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + return otherwise; + } + + [Pure] + public static async Task MatchAsync( this T value, T t1, Func> f1, T t2, Func> f2, @@ -2810,6 +3421,88 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 14 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -3178,6 +3871,93 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 15 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -3568,6 +4348,98 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 16 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + T t16, TResult f16, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + if (value is not null && value.Equals(t16)) + { + return f16; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -3980,6 +4852,103 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 17 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + T t16, TResult f16, + T t17, TResult f17, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + if (value is not null && value.Equals(t16)) + { + return f16; + } + if (value is not null && value.Equals(t17)) + { + return f17; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -4414,6 +5383,108 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 18 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + T t16, TResult f16, + T t17, TResult f17, + T t18, TResult f18, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + if (value is not null && value.Equals(t16)) + { + return f16; + } + if (value is not null && value.Equals(t17)) + { + return f17; + } + if (value is not null && value.Equals(t18)) + { + return f18; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -4870,6 +5941,113 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 19 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + T t16, TResult f16, + T t17, TResult f17, + T t18, TResult f18, + T t19, TResult f19, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + if (value is not null && value.Equals(t16)) + { + return f16; + } + if (value is not null && value.Equals(t17)) + { + return f17; + } + if (value is not null && value.Equals(t18)) + { + return f18; + } + if (value is not null && value.Equals(t19)) + { + return f19; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, @@ -5348,6 +6526,118 @@ public static TResult Match( throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the 20 specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, + T t1, TResult f1, + T t2, TResult f2, + T t3, TResult f3, + T t4, TResult f4, + T t5, TResult f5, + T t6, TResult f6, + T t7, TResult f7, + T t8, TResult f8, + T t9, TResult f9, + T t10, TResult f10, + T t11, TResult f11, + T t12, TResult f12, + T t13, TResult f13, + T t14, TResult f14, + T t15, TResult f15, + T t16, TResult f16, + T t17, TResult f17, + T t18, TResult f18, + T t19, TResult f19, + T t20, TResult f20, + TResult otherwise = default(TResult)) + where T: IEquatable + { + if (value is not null && value.Equals(t1)) + { + return f1; + } + if (value is not null && value.Equals(t2)) + { + return f2; + } + if (value is not null && value.Equals(t3)) + { + return f3; + } + if (value is not null && value.Equals(t4)) + { + return f4; + } + if (value is not null && value.Equals(t5)) + { + return f5; + } + if (value is not null && value.Equals(t6)) + { + return f6; + } + if (value is not null && value.Equals(t7)) + { + return f7; + } + if (value is not null && value.Equals(t8)) + { + return f8; + } + if (value is not null && value.Equals(t9)) + { + return f9; + } + if (value is not null && value.Equals(t10)) + { + return f10; + } + if (value is not null && value.Equals(t11)) + { + return f11; + } + if (value is not null && value.Equals(t12)) + { + return f12; + } + if (value is not null && value.Equals(t13)) + { + return f13; + } + if (value is not null && value.Equals(t14)) + { + return f14; + } + if (value is not null && value.Equals(t15)) + { + return f15; + } + if (value is not null && value.Equals(t16)) + { + return f16; + } + if (value is not null && value.Equals(t17)) + { + return f17; + } + if (value is not null && value.Equals(t18)) + { + return f18; + } + if (value is not null && value.Equals(t19)) + { + return f19; + } + if (value is not null && value.Equals(t20)) + { + return f20; + } + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value, diff --git a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.tt b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.tt index 8a43fc9..fb93a86 100644 --- a/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.tt +++ b/src/FuncSharp/Extensions/ObjectExtensions_ValueMatch.tt @@ -36,6 +36,25 @@ public static partial class ObjectExtensions throw new ArgumentException("The value " + value.SafeToString() + " does not match any of the <#= i #> specified values."); } + /// + /// Matches the value with the specified parameters and returns result of the corresponding value. + /// + [Pure] + public static TResult Match( + this T value, +<#= Lines(i, j => Indent(12) + "T " + Value(j) + ", TResult f" + j, separator: ",") #>, + TResult otherwise = default(TResult)) + where T: IEquatable + { +<# for (var j = 1; j <= i; j++) { #> + if (value is not null && value.Equals(<#= Value(j) #>)) + { + return f<#= j #>; + } +<# } #> + return otherwise; + } + [Pure] public static async Task MatchAsync( this T value,