sumtype 0.2.0
A sum type with pattern matching
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
,@nogc
, andnothrow
when applicable. - No runtime dependency on
TypeInfo
. - No heap allocation/boxing.
Documentation
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.
- 0.2.0 released 6 years ago
- pbackus/sumtype
- MIT
- Copyright © 2018, Paul Backus
- Authors:
- 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 - Download Stats:
-
-
16 downloads today
-
144 downloads this week
-
603 downloads this month
-
165668 downloads total
-
- Score:
- 4.5
- Short URL:
- sumtype.dub.pm