Cannot interpret struct at compile time

Robert Clipsham robert at octarineparrot.com
Sat May 7 17:35:35 PDT 2011


On 08/05/2011 00:39, Andrej Mitrovic wrote:
> One simplistic solution is to use alias this to simulate the same type:
>
> struct Foo
> {
>      int x, y;
> }
>
> string structClone(T)()
> {
>      return "struct " ~ T.stringof ~ "_ { "
>          ~ T.stringof ~ " _inner;
>          alias _inner this;
>          this(T...)(T t) { _inner = typeof(_inner)(t);  } };";
> }
>
> void main()
> {
>      mixin(structClone!Foo);
>      Foo_ foo = Foo_(1, 2);
>
>      assert(foo.x == 1);
>      assert(foo.y == 2);
> }
>
> Field initializations and ctors will work thanks to the templated ctor
> that just forwards to the _inner struct.

Unfortunately this won't do what I need - I need to be able to iterate 
over the members of the resulting struct using .tupleof, which I can't 
do with alias this.

-- 
Robert
http://octarineparrot.com/


More information about the Digitalmars-d-learn mailing list