diff --git a/SuccincT/PatternMatchers/Any.cs b/SuccincT/PatternMatchers/Any.cs index f7731c8..920de6c 100644 --- a/SuccincT/PatternMatchers/Any.cs +++ b/SuccincT/PatternMatchers/Any.cs @@ -23,7 +23,6 @@ public struct Any public static bool operator !=(Any any1, Any any2) => false; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] - // ReSharper disable once ConvertToAutoProperty - public static Any _ => AnAny; + public static Any _ { get; } = AnAny; } } diff --git a/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs index 969ad94..a7e99ef 100644 --- a/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs +++ b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using SuccincT.Functional; namespace SuccincT.PatternMatchers @@ -66,7 +65,7 @@ public EitherTuple(Any value1, Any value2, Any value3) _value3 = new Either(value3); } - public bool MatchesTuple(Tuple tuple) => + public bool MatchesTuple((T1, T2, T3) tuple) => (!_value1.IsLeft || EqualityComparer.Default.Equals(_value1.Left, tuple.Item1)) && (!_value2.IsLeft || EqualityComparer.Default.Equals(_value2.Left, tuple.Item2)) && (!_value3.IsLeft || EqualityComparer.Default.Equals(_value3.Left, tuple.Item3)); diff --git a/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs b/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs index da9a11b..3f3793c 100644 --- a/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs +++ b/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs @@ -33,7 +33,7 @@ public EitherTuple(Any value1, Any value2) _value2 = new Either(value2); } - public bool MatchesTuple(Tuple tuple) => + public bool MatchesTuple((T1, T2) tuple) => (!_value1.IsLeft || EqualityComparer.Default.Equals(_value1.Left, tuple.Item1)) && (!_value2.IsLeft || EqualityComparer.Default.Equals(_value2.Left, tuple.Item2)); } diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs index f7fa12b..7102818 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs @@ -16,16 +16,16 @@ internal sealed class Matcher : IFuncWhereHandler, T1, T2, T3, T4, TResult>, IFuncMatcherAfterElse { - private readonly MatchFunctionSelector, Tuple, TResult> _functionSelector; - private readonly Tuple _item; - private IList> _withValues; - private Func, bool> _whereExpression; - private Func, TResult> _elseFunction; + private readonly MatchFunctionSelector<(T1, T2, T3, T4), (T1, T2, T3, T4), TResult> _functionSelector; + private readonly (T1, T2, T3, T4) _item; + private IList<(T1, T2, T3, T4)> _withValues; + private Func<(T1, T2, T3, T4), bool> _whereExpression; + private Func<(T1, T2, T3, T4), TResult> _elseFunction; - internal Matcher(Tuple item) + internal Matcher((T1, T2, T3, T4) item) { _item = item; - _functionSelector = new MatchFunctionSelector, Tuple, TResult>(x => + _functionSelector = new MatchFunctionSelector<(T1, T2, T3, T4), (T1, T2, T3, T4), TResult>(x => { throw new NoMatchException( $"No match action exists for value of ({_item.Item1}, {_item.Item2}, {_item.Item3}, {_item.Item4}"); @@ -37,21 +37,21 @@ internal Matcher(Tuple item) IActionWithHandler, T1, T2, T3, T4> IMatcher.With( T1 value1, T2 value2, T3 value3, T4 value4) { - _withValues = new List> {Tuple.Create(value1, value2, value3, value4)}; + _withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)}; return this; } IActionWithHandler, T1, T2, T3, T4> IActionMatcher.With( T1 value1, T2 value2, T3 value3, T4 value4) { - _withValues = new List> {Tuple.Create(value1, value2, value3, value4)}; + _withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)}; return this; } IFuncWithHandler, T1, T2, T3, T4, TResult> IFuncMatcher.With(T1 value1, T2 value2, T3 value3, T4 value4) { - _withValues = new List> {Tuple.Create(value1, value2, value3, value4) }; + _withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)}; return this; } @@ -104,14 +104,14 @@ internal Matcher(Tuple item) IFuncWithHandler, T1, T2, T3, T4, TResult>.Or( T1 value1, T2 value2, T3 value3, T4 value4) { - _withValues.Add(Tuple.Create(value1, value2, value3, value4)); + _withValues.Add((value1, value2, value3, value4)); return this; } IActionMatcher IActionWithHandler, T1, T2, T3, T4>.Do( Action action) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), + RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)), _withValues, ActionToFunc(action)); return this; @@ -127,7 +127,7 @@ internal Matcher(Tuple item) IFuncMatcher IFuncWithHandler, T1, T2, T3, T4, TResult>.Do( Func function) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), + RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)), _withValues, x => function(x.Item1, x.Item2, x.Item3, x.Item4)); return this; @@ -136,7 +136,7 @@ internal Matcher(Tuple item) IFuncMatcher IFuncWithHandler, T1, T2, T3, T4, TResult>.Do( TResult value) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), + RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)), _withValues, x => value); return this; @@ -173,15 +173,15 @@ TResult IFuncMatcherAfterElse.Result() return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item); } - private void RecordFunction(Func, IList>, bool> test, - IList> values, - Func, TResult> function) => + private void RecordFunction(Func<(T1, T2, T3, T4), IList<(T1, T2, T3, T4)>, bool> test, + IList<(T1, T2, T3, T4)> values, + Func<(T1, T2, T3, T4), TResult> function) => _functionSelector.AddTestAndAction(test, values, null, function); - private void RecordFunction(Func, bool> test, Func, TResult> function) => + private void RecordFunction(Func<(T1, T2, T3, T4), bool> test, Func<(T1, T2, T3, T4), TResult> function) => _functionSelector.AddTestAndAction(null, null, test, function); - private static Func, TResult> ActionToFunc(Action action) => + private static Func<(T1, T2, T3, T4), TResult> ActionToFunc(Action action) => x => { action(x.Item1, x.Item2, x.Item3, x.Item4); @@ -192,7 +192,7 @@ TResult IFuncMatcherAfterElse.Result() IActionWithHandler, T1, T2, T3, T4>.Or( T1 value1, T2 value2, T3 value3, T4 value4) { - _withValues.Add(Tuple.Create(value1, value2, value3, value4)); + _withValues.Add((value1, value2, value3, value4)); return this; } } diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs index 9b02f93..7a6fd09 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs @@ -16,16 +16,16 @@ internal sealed class Matcher : IFuncWhereHandler, T1, T2, T3, TResult>, IFuncMatcherAfterElse { - private readonly MatchFunctionSelector, EitherTuple, TResult> _functionSelector; - private readonly Tuple _item; + private readonly MatchFunctionSelector<(T1, T2, T3), EitherTuple, TResult> _functionSelector; + private readonly (T1, T2, T3) _item; private IList> _withValues; - private Func, bool> _whereExpression; - private Func, TResult> _elseFunction; + private Func<(T1, T2, T3), bool> _whereExpression; + private Func<(T1, T2, T3), TResult> _elseFunction; - internal Matcher(Tuple item) + internal Matcher((T1, T2, T3) item) { _item = item; - _functionSelector = new MatchFunctionSelector, EitherTuple, TResult>(x => + _functionSelector = new MatchFunctionSelector<(T1, T2, T3), EitherTuple, TResult>(x => { throw new NoMatchException( $"No match action exists for value of ({_item.Item1}, {_item.Item2}, {_item.Item3}"); @@ -288,15 +288,15 @@ TResult IFuncMatcherAfterElse.Result() return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item); } - private void RecordFunction(Func, IList>, bool> test, + private void RecordFunction(Func<(T1, T2, T3), IList>, bool> test, IList> values, - Func, TResult> function) => + Func<(T1, T2, T3), TResult> function) => _functionSelector.AddTestAndAction(test, values, null, function); - private void RecordFunction(Func, bool> test, Func, TResult> function) => + private void RecordFunction(Func<(T1, T2, T3), bool> test, Func<(T1, T2, T3), TResult> function) => _functionSelector.AddTestAndAction(null, null, test, function); - private static Func, TResult> ActionToFunc(Action action) => + private static Func<(T1, T2, T3), TResult> ActionToFunc(Action action) => x => { action(x.Item1, x.Item2, x.Item3); diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs index 0ecfc9c..cc6fbde 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs @@ -16,16 +16,16 @@ internal sealed class Matcher : IFuncWhereHandler, T1, T2, TResult>, IFuncMatcherAfterElse { - private readonly MatchFunctionSelector, EitherTuple, TResult> _functionSelector; - private readonly Tuple _item; + private readonly MatchFunctionSelector<(T1, T2), EitherTuple, TResult> _functionSelector; + private readonly (T1, T2) _item; private IList> _withValues; - private Func, bool> _whereExpression; - private Func, TResult> _elseFunction; + private Func<(T1, T2), bool> _whereExpression; + private Func<(T1, T2), TResult> _elseFunction; - internal Matcher(Tuple item) + internal Matcher((T1, T2) item) { _item = item; - _functionSelector = new MatchFunctionSelector, EitherTuple, TResult>(x => + _functionSelector = new MatchFunctionSelector<(T1, T2), EitherTuple, TResult>(x => { throw new NoMatchException($"No match action exists for value of ({_item.Item1}, {_item.Item2}"); }); @@ -275,15 +275,15 @@ TResult IFuncMatcherAfterElse.Result() return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item); } - private void RecordFunction(Func, IList>, bool> test, + private void RecordFunction(Func<(T1, T2), IList>, bool> test, IList> values, - Func, TResult> function) => + Func<(T1, T2), TResult> function) => _functionSelector.AddTestAndAction(test, values, null, function); - private void RecordFunction(Func, bool> test, Func, TResult> function) => + private void RecordFunction(Func<(T1, T2), bool> test, Func<(T1, T2), TResult> function) => _functionSelector.AddTestAndAction(null, null, test, function); - private static Func, TResult> ActionToFunc(Action action) => + private static Func<(T1, T2), TResult> ActionToFunc(Action action) => x => { action(x.Item1, x.Item2); diff --git a/SuccincT/PatternMatchers/SpecificTypeMatcherExtensions.cs b/SuccincT/PatternMatchers/SpecificTypeMatcherExtensions.cs index 997015a..9827440 100644 --- a/SuccincT/PatternMatchers/SpecificTypeMatcherExtensions.cs +++ b/SuccincT/PatternMatchers/SpecificTypeMatcherExtensions.cs @@ -13,31 +13,34 @@ public static class SpecificTypeMatcherExtensions { public static IMatcher Match(this Tuple item) => new Matcher(item.Item1); - public static IMatcher Match(this Tuple item) => - new Matcher(item); + public static IMatcher Match(this (T1, T2) tuple) => + new Matcher(tuple); + + public static IMatcher Match(this Tuple tuple) => + new Matcher((tuple.Item1, tuple.Item2)); public static IMatcher Match(this ITupleMatchable item) { var tuple = item.PropertiesToMatch; - return new Matcher(tuple); + return new Matcher((tuple.Item1, tuple.Item2)); } public static IMatcher Match(this ITupleMatchable item) { var tuple = item.PropertiesToMatch; - return new Matcher(tuple); + return new Matcher((tuple.Item1, tuple.Item2, tuple.Item3)); } public static IMatcher Match(this ITupleMatchable item) { var tuple = item.PropertiesToMatch; - return new Matcher(tuple); + return new Matcher((tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4)); } - public static IMatcher Match(this Tuple item) => - new Matcher(item); + public static IMatcher Match(this Tuple tuple) => + new Matcher((tuple.Item1, tuple.Item2, tuple.Item3)); - public static IMatcher Match(this Tuple item) => - new Matcher(item); + public static IMatcher Match(this Tuple tuple) => + new Matcher((tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4)); } } \ No newline at end of file diff --git a/SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs b/SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs index 9478f34..d912746 100644 --- a/SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs +++ b/SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs @@ -18,9 +18,18 @@ public void Tuple_CanBeMatchedWithExec() } [Test] - public void TupleWithAnyInt_CanBeMatchedWithExec() + public void ValueTuple_CanBeMatchedWithExec() { - var tuple = Tuple.Create(1, "a"); + var tuple = (1, "a"); + var result = false; + tuple.Match().With(1, "a").Do((x, y) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void ValueTupleWithAnyInt_CanBeMatchedWithExec() + { + var tuple = (1, "a"); var result = false; tuple.Match().With(_, "a").Do((x, y) => result = true).Exec(); Assert.IsTrue(result); @@ -36,9 +45,9 @@ public void TupleWithAnyString_CanBeMatchedWithExec() } [Test] - public void TupleWithAnyAndAny_CanBeMatchedWithExec() + public void ValueTupleWithAnyAndAny_CanBeMatchedWithExec() { - var tuple = Tuple.Create(1, "a"); + var tuple = (1, "a"); var result = false; tuple.Match().With(_, _).Do((x, y) => result = true).Exec(); Assert.IsTrue(result); diff --git a/SuccincTTests/SuccincT/Tuples/TupleTestsT1T2.cs b/SuccincTTests/SuccincT/Tuples/TupleTestsT1T2.cs index 311f492..6ae117d 100644 --- a/SuccincTTests/SuccincT/Tuples/TupleTestsT1T2.cs +++ b/SuccincTTests/SuccincT/Tuples/TupleTestsT1T2.cs @@ -17,9 +17,9 @@ public void Tuple_CanBeMatched() } [Test] - public void Tuple_CanBeMatchedViaOr() + public void ValueTuple_CanBeMatchedViaOr() { - var tuple = Tuple.Create(1, "a"); + var tuple = (1, "a"); var result = tuple.Match().To().With(2, "a").Or(1, "a").Do((x, y) => true).Result(); Assert.IsTrue(result); } @@ -33,9 +33,9 @@ public void Tuple_CanBeMatchedUsingIntWildcard() } [Test] - public void Tuple_CanBeMatchedUsingStringWildcard() + public void ValueTuple_CanBeMatchedUsingStringWildcard() { - var tuple = Tuple.Create(1, "a"); + var tuple = (1, "a"); var result = tuple.Match().To().With(1, _).Do((x, y) => true).Result(); Assert.IsTrue(result); }