On Sunday, 1 October 2017 at 02:29:57 UTC, Jonathan M Davis wrote:
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
I think I've almost got it:
string generateGetInfo( /*alias*/ Info,alias func,args...)()
{
string code;
foreach(field; AliasSeq!(Info.tupleof))
{
code ~= "@property " ~ typeof(field).stringof ~ " " ~
field.stringof ~ "()" ~
"{ " ~
" typeof(return) ret;" ~
func.stringof ~ "(" ~ args.stringof ~ "," ~
__traits(getAttributes, field).stringof ~
",ret.sizeof,&ret,null);" ~
"return ret; " ~
"}";
}
}
return code;
}
struct MyType
{
void* raw;
static struct Info
{
@(42) int foo;
}
mixin(generateGetInfo!(Info,MyTypeGetInfo,raw));//errors out
pragma(msg,generateGetInfo!(Info,MyTypeGetInfo,raw)); // also
errors out
}
but this gives a bunch of
Error: need 'this' for 'foo' of type 'int'.
even with the pragma msg.
I dont see why though:
a) the Info struct is static
b) I'm only using .stringof on the _type's_ fields
c) its got the name in the errors message! it just wont let
me use it.
The latter doesn't really work though when the arguments are an
AliasSeq of fields like you get from tupleof.
Why is that and is that what is causing the problem here?
Thanks
Nic