xxxxxxxxxx
import std.variant, std.stdio,std.typecons,std.meta;
template Shape() {
long x;
long y;
}
alias AllShapes = AliasSeq!(Cyrcle,Square,Rect);
struct AnyShape {
Algebraic!AllShapes shape;
long opDispatch(string field)() {
static foreach( P; AllShapes ) {
if(shape.peek!P) return mixin( q{shape.get!P.} ~ field );
}
assert(false);
}
void opAssign( S )( S shape ) {
this.shape = shape;
}
}
struct Cyrcle {
mixin Shape;
long radius;
}
struct Square {
mixin Shape;
long size;
}
struct Rect {
mixin Shape;
long width;
long height;
}
void main()
{
AnyShape[3] shapes;
shapes[0] = Cyrcle(1,2,3);
shapes[1] = Square(4,5,6);
shapes[2] = Rect(7,8,9,0);
shapes[0].x.writeln;
shapes[1].y.writeln;
//shapes[2].width.writeln;
shapes.sizeof.writeln;
Cyrcle.sizeof.writeln;
Square.sizeof.writeln;
Rect.sizeof.writeln;
}