Runtime error trying to call opCall on variant array of objects

aliak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 24 08:36:56 PST 2016


Hey, so I'm trying to make an variant array of objects (each one 
has an opCall defined) and then call each of them in succession. 
It doesn't seem to be working. The error I get is:

"Cannot apply `()' to a value of type `Command!(__lambda1, int)". 
I think it's failing when it checks for "!isFunctionPointer!A && 
!isDelegate!A".

Here's the bit of code:

struct Command(alias fun, Args...) {
     Args args;
     this(Args args) {
         args = args;
     }
     auto opCall() {
         return fun(args);
     }
}

auto command(alias fun, Args...)(Args args) {
     return Command!(fun, Args)(args);
}

void main() {
     auto commands = variantArray(
         command!(a => a + 1)(1),
         command!(a => a + 2)(1),
         command!(a => a + 3)(1),
     );

    // commands.each!(a => a()); // <-- crashes runtime

     typeid(commands[0]).writeln; // 
std.variant.VariantN!32LU.VariantN

     auto cmd = command!(a => a + 1)(1);
     typeid(cmd).writeln; // scratchpad.main.Command!(__lambda4, 
int).Command
}

Is there a way to do what I'm trying to do?

Cheers!


More information about the Digitalmars-d-learn mailing list