Thread overview
[Issue 10490] Type enum in std.variant.Algebraic for final switches
Apr 17, 2015
Justin Whear
Apr 17, 2015
Justin Whear
Dec 17, 2022
Iain Buclaw
April 17, 2015
https://issues.dlang.org/show_bug.cgi?id=10490

Justin Whear <justin@economicmodeling.com> changed:

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

--- Comment #2 from Justin Whear <justin@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);
}
----

--
April 17, 2015
https://issues.dlang.org/show_bug.cgi?id=10490

--- Comment #3 from Justin Whear <justin@economicmodeling.com> ---
Oops, that should use `value.peek!T`, not `value.get!T`

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=10490

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4

--