-
Notifications
You must be signed in to change notification settings - Fork 15
Home
Current release: v1.4.0
v1.4.0
-
Yet another small breaking change. Please note that if you have previously used pattern matching on "normal" types (ie, not
Options
,Unions
orValueOrError
types), then you'll have to addusing SuccincT.PatternMatchers.GeneralMatcher;
alongsideusing SuccincT.PatternMatchers;
to each class using this functionality. The reason for this is that the generic type pattern matcher had to move to a new namespace to ensure proper use of the tuple matchers, whereas the types generated by the matcher fluent syntax remain in the old namespace. Please see this Stack Overflow question and answer for details on why this had to happen. -
Support added for pattern matching
Tuple<T>
,Tuple<T1,T2>
,Tuple<T1,T2,T3>
andTuple<T1,T2,T3,T4>
. See the Pattern matching tuples page for more details. -
A new interface,
ITupleMatchable
has been introduced, with the specific aim of allowing the tuple matching features to be applied to other "value types"/"DTO's"/"POCOs"/entities etc (call them what you like [caveat that all the previous things are technically different accepted]). The idea being that a type containing up to four items of data can implementITupleMatchable
in order to expose that data as a tuple, which is then matched using normal tuple matching rules. See theITupleMatchable
page for more details.
Release notes for previous versions
Succinc<T> provides a set of union types (Union<T1, T2>
, Union<T1, T2, T3>
and Union<T1, T2, T3, T4>
) where an instance will hold exactly one value of one of the specified types. In addition, it provides the likes of Option<T>
that can have the value Some<T>
or None
.
Succinc<T> uses Option<T>
to provide replacements for the .NET basic types' TryParse()
methods and Enum.Parse()
. In all cases, these are extension methods to string
and they return Some<T>
on a successful parse and None
when the string is not a valid value for that type. No more out
parameters!
Succinc<T> can pattern match values, tuples, unions etc in a way similar to F#'s pattern matching features. It uses a fluent syntax to achieve this. Some examples of its abilities:
public static void PrintColorName(Color color)
{
color.Match()
.With(Color.Red).Do(x => Console.WriteLine("Red"))
.With(Color.Green).Do(x => Console.WriteLine("Green"))
.With(Color.Blue).Do(x => Console.WriteLine("Blue"))
.Exec();
}
public static string SinglePositiveOddDigitReporter(Option<int> data)
{
return data.Match<string>()
.Some().Of(0).Do(x => "0 isn't positive or negative")
.Some().Where(x => x == 1 || x == 3 || x == 5 || x == 7 || x == 9).Do(x => x.ToString())
.Some().Where(x => x > 9).Do(x => string.Format("{0} isn't 1 digit", x))
.Some().Where(x => x < 0).Do(i => string.Format("{0} isn't positive", i))
.Some().Do(x => string.Format("{0} isn't odd", x))
.None().Do(() => string.Format("There was no value"))
.Result();
}
See the Succinc<T> pattern matching guide for more details.
Succinc<T> supports partial function applications. A parameter can be supplied to a multi-parameter method and a new function will be returned that takes the remaining parameters. For example:
Func<int,int> times = (p1, p2) => p1 * p2;
var times8 = times.Apply(8);
var result = times8(9); // <- result == 72
See the Succinc<T> partial applications guide for more details.
The following types are defined by Succinc<T>.
Action
/Func
conversionsCycle
methods- Converting between
Action
andFunc
- Extension methods for existing types that use
Option<T>
- Indexed enumerations
IEnumerable<T>
cons- Option-based parsers
- Partial function applications
- Pattern matching
- Pipe Operators
- Typed lambdas
Any
Either<TLeft,TRight>
None
Option<T>
Success<T>
Union<T1,T2>
Union<T1,T2,T3>
Union<T1,T2,T3,T4>
Unit
ValueOrError