This code:
==============
module scratch;
import std.variant;
unittest {
Variant v;
int foo() { return 1; }
v = &foo;
v();
}
================
Produces this error:
================
$ dmd -unittest -run bronze/util/scratch.d
bronze\util\scratch.d(12): Error: struct VariantN does not overload ()
===============
IMO, this is a design flaw: Variants claim to support function and delegate pointers, and it seems obvious that supporting them should support invoking them. (In particular, since functions and delegates are different sizes, Variants should be very useful in this role, except ...)
Comment #1 by sandford — 2011-01-09T08:43:29Z
Note that as listed in Bug 4053, struct constructors and opCall are not compatible, so the v() syntax isn't possible. In the update to variant I've been working on, I've added a .call method, which supports functions, delegates, opCall, etc. Also, multi-argument opCall should be supported:
unittest {
Variant v;
int foo(int x, int y) { return x+y; }
v = &foo;
assert(v(4,6) == 10);
}
See https://jshare.johnshopkins.edu/rjacque2/public_html/variant.mht