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.9.3 2019-Nov-22
0.9.2 2019-Oct-09
0.9.1 2019-Sep-30
0.9.0 2019-Sep-28
0.8.13 2019-Sep-11
Show all 37 versions
Download Stats:
  • 97 downloads today

  • 319 downloads this week

  • 1329 downloads this month

  • 10493 downloads total

Score:
4.5
Short URL:
sumtype.dub.pm