Skip to content
David Arno edited this page Jul 1, 2015 · 34 revisions

Succinc<T>

Discriminated unions, pattern matching and partial applications for C#


Current release: v1.3.1

Release Notes

v1.3.1

  1. Fix for bug Union<T1, T2> comparisons are broken
  2. Fix for bug value equality for Union<T1, T2, T3> and Union<T1, T2, T3> hasn't been implemented

v1.3.0

  1. Union<T1,T2,T3,T4> now supports pattern matching.
  2. For patterns that return a value (and thus invoke a Func<> on match) now support both lambdas/methods and simple expressions for both Do and Else. See the new Pattern Matching Section of the wiki for more details.
  3. Simple union creator factories have been implemented that require the type parameters for multiple unions of the same type to be specified just once. Please see the Union Creator wiki page for more info (don't worry, no singletons or service locators were used in creating this feature!).
  4. Various code improvements, including trying to consistently using TResult instead of TReturn and better comments.
  5. The wiki has been significantly expanded. This isn't strictly part of the release, but is published at the same time.
  6. Finally, I have remembered to tag the git repos for both the code and the wiki with 1.3.0, so in future I'll know what changed after the release.

Release notes for previous versions


Features

Discriminated Unions

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!

Pattern Matching

Succinc<T> can pattern match values, 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.

Partial Applications

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.

Types

The following types are defined by Succinc<T>.