Variant is just a class
Josphe Brigmo
JospheBrigmo at gmail.com
Sat Sep 8 02:09:32 UTC 2018
Here is a working example:
import std.stdio;
class Project(Wrapped, Interface) : Interface
{
import std.traits;
Wrapped wrapped;
static foreach (member; __traits(allMembers, Interface))
{
static foreach (overload; __traits(getOverloads, Interface,
member))
{
mixin(`ReturnType!overload ` ~ member ~ `(Parameters!overload
params) { return wrapped.` ~ member ~ `(params); }`);
}
}
//this(Wrapped w)
static Interface opCall(Wrapped w)
{
auto t = new typeof(this);
t.wrapped = w;
return t;
}
}
interface I
{
void foo();
}
class A
{
void foo() { writeln("A"); }
}
class B
{
void foo() { writeln("B"); }
}
void main()
{
auto b = new B();
auto a = new A();
auto x = Project!(A, I)(a);
auto y = Project!(B, I)(b);
a.foo();
b.foo();
x.foo();
y.foo();
}
More information about the Digitalmars-d
mailing list