Function traits at compile time

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Mon Jun 12 11:51:33 PDT 2006


Oskar Linde wrote:
> 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.
> 

"thank you for coming up with"... What do you mean? You already saw 
Tomasz S's code 
(news://news.digitalmars.com:119/e5kdls$bic$1@digitaldaemon.com) which 
does the same (in fact it's even a bit better since it is shorter due to 
that use of default argument values). Or am I missing something? 
(feeling sleep-deprived today :o )


-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list