www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16662] New: Can't call std.variant.visit from a pure function


          Issue ID: 16662
           Summary: Can't call std.variant.visit from a pure function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

Under dmd 2.072, calling std.variant.visit from inside a pure function is
forbidden, even if all of the functions used to instantiate visit are
themselves pure.

Example code: 

import std.variant;

alias IntOrString = Algebraic!(int, string);

IntOrString twice(IntOrString v) pure {
    return v.visit!(
        (int n) pure => IntOrString(2*n),
        (string s) pure => IntOrString(s ~ s)

...and the compiler error:

Error: pure function 'test2.twice' cannot call impure function
'std.variant.visit!(function (int n) => VariantN(& handler, cast(ubyte)0u,
).this(2 * n), function (string s) => VariantN(& handler, cast(ubyte)0u,
).this(s ~ s)).visit!(VariantN!(16LU, int, string)).visit'

Nov 04 2016