sumtype 0.1.0

A sum type for modern D


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

Manual usage
Put the following dependency into your project's dependences section:

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:
1.2.8 2024-Mar-03
1.2.7 2022-Sep-05
1.2.6 2022-Jul-27
1.2.5 2022-Jun-21
1.2.4 2022-May-12
Show all 59 versions
Download Stats:
  • 16 downloads today

  • 144 downloads this week

  • 603 downloads this month

  • 165668 downloads total

Score:
4.5
Short URL:
sumtype.dub.pm