vibe.d beta banner
get vibe.d
0.10.1

Asynchronous I/O that doesn’t get in your way, written in D

Function apply

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

auto apply(alias handler, TA) (
  TA ta
)
if (isInstanceOf!(TaggedAlgebraic, TA));

auto apply(alias handler, T) (
  T value
)
if (!isInstanceOf!(TaggedAlgebraic, T));

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

Returns

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

Example

union U {
	int i;
	string s;
}
alias TA = TaggedAlgebraic!U;

assert(TA(12).apply!((v) {
	static if (is(typeof(v) == int)) {
		assert(v == 12);
		return 1;
	} else {
		return 0;
	}
}) == 1);

assert(TA("foo").apply!((v) {
	static if (is(typeof(v) == string)) {
		assert(v == "foo");
		return 2;
	} else {
		return 0;
	}
}) == 2);

"baz".apply!((v) {
	assert(v == "baz");
});
}


/// User-defined attibute to disable `opIndex` forwarding for a particular tagged union member.
@property auto disableIndex() { assert(__ctfe, "disableIndex must only be used as an attribute."); return DisableOpAttribute(OpKind.index, null); 
Authors

Sönke Ludwig

Copyright

Copyright 2015-2019, Sönke Ludwig.

License

www.boost.org/LICENSE_1_0.txt, Boost License 1.0.