sumtype 0.1.1

A sum type with pattern matching


To use this package, run the following command in your project's root directory:

sumtype

A sum type for modern D.

Features

  • Pattern matching.
  • Self-referential types (This).
  • Full attribute-correctness—pure, @safe, and @nogc where applicable.
  • No runtime dependency on TypeInfo.

Example

import std.typecons: Tuple, tuple;

struct Nil {}
alias List = SumType!(
    Nil,
    Tuple!(int, "head", This*, "tail")
);
alias Cons = Tuple!(int, "head", List*, "tail");

List* nil()
{
    return new List(Nil());
}

List* cons(int item, List* l)
{
    return new List(Cons(item, l));
}

List* list(int[] items...)
{
    if (items.length == 0)
        return nil;
    else
        return cons(items[0], list(items[1..$]));
}

int sum(List l)
{
    return l.match!(
        (Nil _) => 0,
        (Cons cons) => cons.head + sum(*cons.tail)
    );
}

List* myList = list(1, 2, 3, 4, 5);

assert(sum(*myList) == 15);


Installation

If you're using dub, add the sumtype package to your project as a dependency.

Alternatively, since it's a single, self-contained module, you can simply copy sumtype.d to your source directory and compile as usual.

Authors:
  • Paul Backus
Dependencies:
none
Versions:
0.8.12 2019-Aug-14
0.8.11 2019-Aug-01
0.8.10 2019-Jul-31
0.8.9 2019-Jun-22
0.8.8 2019-Jun-21
Show all 32 versions
Download Stats:
  • 18 downloads today

  • 208 downloads this week

  • 907 downloads this month

  • 7078 downloads total

Score:
3.8
Short URL:
sumtype.dub.pm