[Issue 10490] Type enum in std.variant.Algebraic for final switches

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Apr 17 11:39:29 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=10490

Justin Whear <justin at economicmodeling.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |justin at economicmodeling.com

--- Comment #2 from Justin Whear <justin at economicmodeling.com> ---
I've been using this solution, perhaps it should be included in std.variant,
though possibly with a better name:

----
import std.variant;
import std.traits : isInstanceOf;

/**
 * Calls the correct overload of Fun based on the runtime value of the Variant
value.
 */
auto applyToAlgebraic(alias Fun, Value)(Value value)
    if (isInstanceOf!(VariantN, Value))  // Can we constrain to Algebraic only?
{
    foreach (T; Value.AllowedTypes) // unrolled at CT
        if (typeid(T) is value.type)
            return Fun(value.get!T);
    assert(0);
}
----

--


More information about the Digitalmars-d-bugs mailing list