apply

Calls a the given callback with the static type of the contained value.

The handler callback must be a lambda or a single-argument template function that accepts all possible types that the given TaggedAlgebraic can hold.

  1. auto apply(TA ta)
    apply
    (
    alias handler
    TA
    )
    (
    TA ta
    )
    if (
    isInstanceOf!(TaggedAlgebraic, TA)
    )
  2. auto apply(T value)

Return Value

Type: auto

If handler has a non-void return value, its return value gets forwarded to the caller.

Examples

1 union U {
2 	int i;
3 	string s;
4 }
5 alias TA = TaggedAlgebraic!U;
6 
7 assert(TA(12).apply!((v) {
8 	static if (is(typeof(v) == int)) {
9 		assert(v == 12);
10 		return 1;
11 	} else {
12 		return 0;
13 	}
14 }) == 1);
15 
16 assert(TA("foo").apply!((v) {
17 	static if (is(typeof(v) == string)) {
18 		assert(v == "foo");
19 		return 2;
20 	} else {
21 		return 0;
22 	}
23 }) == 2);
24 
25 "baz".apply!((v) {
26 	assert(v == "baz");
27 });

Meta