loop through specific class members
Sergey Gromov
snake.scaly at gmail.com
Sun Jan 25 16:49:35 PST 2009
Wed, 21 Jan 2009 12:48:07 +0100, Trass3r wrote:
> Christopher Wright schrieb:
>> On the other hand, you can get the non-final, non-private methods of a
>> class with something like:
>> foreach (member; __traits (allMembers, Class))
>> {
>> foreach (overload; __traits (getVirtualFunctions, Class, member))
>> {
>> // do stuff
>> }
>> }
>>
>> Naturally, __traits and foreach don't mix, so you'll have to use
>> template recursion. This is pretty damn ugly.
>>
>
> But why doesn't that work? It doesn't even work when forced to run at
> compile time by CTFE.
> Had a look at the compiler code, it uses the same mechanism as
> pragma(msg, for example, so shouldn't something like the above
> theoretically be possible?
foreach() is a runtime construct. It may be *interpreted* at
compile-time, but it's interpreted as if it were run time nevertheless.
It dynamically changes the value of 'member' variable.
pragma() and __traits() are compile-time constructs. They are fully and
statically expanded before anything is being interpreted and cannot
handle dynamically changing variables.
More information about the Digitalmars-d-learn
mailing list