TrapFlow - Pattern Matching Library
Zenw
zero.error.no.warning at gmail.com
Sun Nov 9 10:40:36 UTC 2025
Hello!
I've created a pattern matching library for D. Still
experimental, but might be an interesting approach.
https://github.com/Zero-error-no-warning/TrapFlow
```D
import trapflow;
// Basic usage
auto x = 10.flow!string
.trap[5]("5")
.trap[$ <= 4](" <= 4")
.trap[10,12]("10 12")
.trap[15 .. 20]("15 16 17 18 19")
.trap[$]("other")
.result;
// Result: "10 12"
struct Inner { int a; string b; }
struct Outer { Inner s; int t; }
auto x = Outer(Inner(3,"three"), 33).flow("default")
.trap[$[$[1,"one"], $]]("case 1")
.trap[$[$ , 22]]("case 2")
.trap[$[$[$,"three"], 33]]("case 3")
.result;
// Result: "case 3"
// FizzBuzz
foreach(idx; 1..10) {
auto fizzbuzz = tuple(idx % 3, idx % 5).flow!string
.trap[$[0,0]]("FizzBuzz")
.trap[$[0,$]]("Fizz")
.trap[$[$,0]]("Buzz")
.trap[$](idx.to!string)
.result;
}
```
Looking forward to feedback :)
More information about the Digitalmars-d-announce
mailing list