View source code
Display the source code in std/sumtype.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Template std.sumtype.has

Checks whether a SumType contains a value of a given type.

template has(T);

The types must match exactly, without implicit conversions.

Contained Functions

NameDescription
has The actual has function.

Parameters

NameDescription
T the type to check for.

Example

Basic usage

SumType!(string, double) example = "hello";

assert( example.has!string);
assert(!example.has!double);

// If T isn't part of the SumType, has!T will always return false.
assert(!example.has!int);

Example

With type qualifiers

alias Example = SumType!(string, double);

Example m = "mutable";
const Example c = "const";
immutable Example i = "immutable";

assert( m.has!string);
assert(!m.has!(const(string)));
assert(!m.has!(immutable(string)));

assert(!c.has!string);
assert( c.has!(const(string)));
assert(!c.has!(immutable(string)));

assert(!i.has!string);
assert(!i.has!(const(string)));
assert( i.has!(immutable(string)));
}

/// As a predicate
version (D_BetterC) {

Example

As a predicate

import std.algorithm.iteration : filter;
import std.algorithm.comparison : equal;

alias Example = SumType!(string, double);

auto arr = [
    Example("foo"),
    Example(0),
    Example("bar"),
    Example(1),
    Example(2),
    Example("baz")
];

auto strings = arr.filter!(has!string);
auto nums = arr.filter!(has!double);

assert(strings.equal([Example("foo"), Example("bar"), Example("baz")]));
assert(nums.equal([Example(0), Example(1), Example(2)]));

Authors

Paul Backus

License

Boost License 1.0