Function traits at compile time

Oskar Linde oskar.lindeREM at OVEgmail.com
Mon Jun 12 08:58:54 PDT 2006


Daniel Keep skrev:
> Victor Nakoryakov wrote:
>> Hi all,
> 
> Hi.
> 
>> Consider code:
>>
>> class MyClass(StructT)
>> {
>> ....
>> }
>>
>> I know, that StructT has a function `foo` in it, but I don't know number
>> of parameters and their types, of course. Does anybody knows a way to
>> extract types to use them in `static if ( is(...) )` statements?
> 
> Ok, this is ugly, and evil, evil hack, but it works.  The python script
> that generated it is included if you need more and/or less arguments.
> 

Nice set of templates. Makes one wish D had variadic templates though. :)

> Some examples:
> 
> # NumberOfArgs!(&StructT.foo); // Number of arguments
> # ReturnType!(&StructT.foo);   // Alias to return type
> # ArgType!(&Struct.foo, n);    // Alias to type of 'n'th argument
> #                              // (counting from 1).

I don't think those will work as typed. Unless I'm mistaken, you need 
typeof(&StructT.foo) instead of just &StructT.foo. And unless 
StructT.foo is static, you will need to add delegate (as different from 
function) overloads for ftype.d and use typeof(&StructT.init.foo).

Apart from that, I must thank you for coming up with this inspired way 
of (ab)using the ifti support. I've found it much useful.

Regards,

Oskar



More information about the Digitalmars-d mailing list