sumtype 0.1.0

A sum type for modern D


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;

alias Tree = SumType!(int, Tuple!(This*, "left", This*, "right"));
alias Node = Tuple!(Tree*, "left", Tree*, "right");

Node node(Tree* left, Tree* right)
{
    return tuple!("left", "right")(left, right);
}

int[] inorder(Tree t)
{
    return t.match!(
        (int leaf) => [leaf],
        (Node node) => inorder(*node.left) ~ inorder(*node.right)
    );
}

Tree x =
    Tree(node(
        new Tree(1),
        new Tree(node(
            new Tree(2),
            new Tree(3)
        ))
    ));

assert(inorder(x) == [1, 2, 3]);


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:
  • 14 downloads today

  • 172 downloads this week

  • 950 downloads this month

  • 7215 downloads total

Score:
3.8
Short URL:
sumtype.dub.pm