[Template] Mixins and foreach

Nicholas Wilson iamthewilsonator at hotmail.com
Sun Oct 1 02:07:26 UTC 2017


On Sunday, 1 October 2017 at 01:05:56 UTC, Nicholas Wilson wrote:
> struct MyType
> {
>     void* raw;
>     static struct Info
>     {
>         @(42) int foo;
>     }
>     mixin generateGetInfo!MyTypeGetInfo;
> }
>
> extern(C) void MyTypeGetInfo(void*,int,size_t,void*size_t*);
> mixin template generateGetInfo(alias func)
> {
>     foreach(field; typeof(this).Info.tupleof)
>     {
>         mixin("@property " ~ typeof(field).stringof ~ " " ~ 
> field.stringof ~ "()" ~
>                " { " ~
>                "    typeof(return) ret;" ~
>                func.stringof ~ "(this.tupleof," ~ 
> __traits(getAttributes, field)[0] ~ ",ret.sizeof,&ret,null);" ~
>               "return ret; }");
>     }
> }
>
> Fails with
> Error: declaration expected, not 'foreach'
> Error: no identifier for declarator typeof(this).Info.tupleof
> Error: declaration expected, not ')'
>
> I also tried a double mixin, one for the foreach and one for 
> the mixin to no avail. How do I do a foreach in a [template] 
> mixin to procedurally inject multiple declarations ? Static 
> foreach is not an option as I am using LDC.

Hmm, generating the mixed in code as all one string and then 
mixing it in should work I think. Rather ugly though.


More information about the Digitalmars-d-learn mailing list