On Sunday, October 01, 2017 02:07:26 Nicholas Wilson via Digitalmars-d-learn 
wrote:
> 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.

I would have thought that it would be pretty straightforward to just write a
recursive, eponymous template to solve the problem and have it recursively
build a single string to mix in for everything.

In general though, without static foreach, you're either going to be using
templates to operate in a functional manner rather than a procedural one, or
you're going to be writing procedural code that generates a string to mix
in. The latter doesn't really work though when the arguments are an AliasSeq
of fields like you get from tupleof.

This looks like it would be pretty straightforward to do with a recursive
template though.

- Jonathan M Davis

Reply via email to