Created
April 26, 2022 18:49
-
-
Save crazymonkyyy/4706abc1e77cdb2c17b848c36da86c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct nullable(T){ | |
T me; alias me this; | |
bool isnull=true; | |
this(T t){ | |
me=t; | |
isnull=false; | |
}} | |
template match(A...){ | |
auto match(T...)(T args){ | |
static foreach(f;A[0..$-1]){ { | |
auto o=f(args); | |
if(! o.isnull){return o.me;} | |
}} | |
return A[$-1](args); | |
} | |
} | |
unittest{ | |
import std; | |
alias O=nullable!string; | |
O foo(int i){ | |
if(i%3==0){return O("foo");} | |
return O(); | |
} | |
O bar(int i){ | |
if(i%5==0){return O("bar");} | |
return O(); | |
} | |
O foobar(int i){ | |
if(i%15==0){return O("foobar");} | |
return O(); | |
} | |
string litteral(int i){ | |
return i.to!string;} | |
foreach(i;1..100){ | |
i.match!(foobar,foo,bar,litteral).writeln; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment