Result handing in D: some|none

9il ilyayaroshenko at gmail.com
Sun Jan 30 11:02:17 UTC 2022


On Sunday, 30 January 2022 at 10:58:33 UTC, 9il wrote:
> [mir.algebraic](http://mir-core.libmir.org/mir_algebraic.html)

I like `suit` a lot. It helps to define very clean visitors.

```d
import std.traits: isDynamicArray, Unqual;
import std.meta: templateNot;
alias V = Variant!(long, int, string, long[], int[]);
alias autoGetElementType = match!(
     (string s) => "string", // we override the suit handler below 
for string
     suit!(isDynamicArray, a => Unqual!(typeof(a[0])).stringof),
     suit!(templateNot!isDynamicArray, a => 
Unqual!(typeof(a)).stringof),
);
assert(autoGetElementType(V(string.init)) == "string");
assert(autoGetElementType(V((long[]).init)) == "long");
assert(autoGetElementType(V((int[]).init)) == "int");
assert(autoGetElementType(V(long.init)) == "long");
assert(autoGetElementType(V(int.init)) == "int");
```



More information about the Digitalmars-d-announce mailing list