Is std.variant.visit not @nogc?

helxi brucewayneshit at gmail.com
Mon Apr 9 03:20:58 UTC 2018


import std.variant, core.stdc.stdio;

Algebraic!(T, string) fib_nth(T)(T n)
{
     return n % 15
                 ? n % 5
                     ? n % 3
                         ? Algebraic!(T, string)(n)
                         : Algebraic!(T, string)("Fizz")
                     : Algebraic!(T, string)("Buzz")
                 : Algebraic!(T, string)("Fizzbuzz");
}

void main() @nogc
{
     foreach (i; 1 .. 101)
     {
         fib_nth(i).visit!(
             (string s) => printf("%s\n", s.ptr),
             (int n) => printf("%i\n", n)
         );
     }
}


Complains source/app.d(18,19): Error: @nogc function D main 
cannot call non- at nogc function std.variant.visit!(function 
(string s) => printf("%s\x0a", cast(immutable(char)*)s), function 
(int n) => printf("%i\x0a", n)).visit!(VariantN!(16LU, int, 
string)).visit
/usr/bin/dmd failed with exit code 1.


If so, is there a way to emulate `visit` in a @nogc setting?


More information about the Digitalmars-d-learn mailing list