__traits and determining all overridable functions
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 12 09:05:58 PST 2007
"Christopher Wright" wrote
> http://dsource.org/projects/dmocks/browser/trunk/dmocks
> Take a look at MockObject.d and MethodMock.d -- they have pretty much
> everything you need. Maybe I should extract them out....
Excellent work! I think this is exactly what I was looking for.
>> However, in doing so I find that the function does not work correctly,
>> and in addition, the argument to __traits for isVirtualFunction does not
>> take a string, but the result of __traits allMembers is an array of
>> strings. I think it would be more useful if isVirtualFunction and the
>> other isXX would take either a string or the direct statement. For
>> example:
>>
>> __traits(isVirtualFunction, "A.foo")
>> would be the same as:
>> __traits(isVirtualFunction, A.foo)
>
> class A {
> final void foo ();
> int foo (int i);
> }
>
> What to return?
> __traits(isVirtualFunction, A.foo) is ambiguous as well. I don't recommend
> using it, and I don't use it.
Hm... good point. So the getVirtualFunctions would be a better idea.
>
>> Then I could do:
>>
>> foreach(x; __traits(allMembers, A))
>> if(__traits(isVirtualfunction, "A." ~ x))
>> // append to string that redefines virtual function.
>
> You can't foreach an array at compile time, and __traits(allMembers)
> returns an array. You can index an array at compile time, but again, you
> can't use a while loop at compile time.
>
> You have to use recursion.
Aha! this is what I was missing :)
>
> Eventually, static foreach should be able to do this. Or you can bug
> Walter about changing allMembers to return a tuple of strings.
>
>> Second, the mixin doesn't work. I hard-coded the function to output the
>> string I was trying to build. Therefore, the compiler should be able to
>> execute the method in compile time, and pre-determine the string to
>> return. But the mixin just defines the function name that I am calling as
>> a member. i.e., I define my function above as:
>>
>> string redefFunctions(T)();
>>
>> and my mixin in the class is:
>>
>> mixin(redefFunctions!(A)());
>>
>> But when I build my class, there is a member redefFunctions!(A) instead
>> of the list of virtual functions I expected to override.
>
> Not sure what the problem there is. If you omit the parentheses around the
> template method call, you'll definitely get the result you describe (that
> had me hung up for a bit), but your example has the parentheses.
Not sure either :) But enough about my horribly disfigured abortion of
code, I think I'll just base my stuff off of your Mock Objects lib :)
-Steve
More information about the Digitalmars-d
mailing list