toString multiple overrides

Timon Gehr timon.gehr at gmx.ch
Sat Feb 11 05:56:01 PST 2012


On 02/11/2012 08:48 AM, Jonathan M Davis wrote:
> On Friday, February 10, 2012 22:41:20 Ellery Newcomer wrote:
>> dmd 2.057
>>
>> Two mixin templates, each define toString, mix them in to your class and ..
>>
>> Error: function test.X.T2!().toString multiple overrides of same function
>>
>> So this behavior is new, but is it sensical?
>>
>>
>> Sample code:
>>
>> mixin template T1(){
>>       string toString(){
>>       return "1";
>>       }
>> }
>> mixin template T2(){
>>       string toString(){
>>       return "2";
>>       }
>> }
>> class X{
>>    mixin T1!() a;
>>    mixin T2!() b;
>> }
>>
>> void main(){
>> }
>
> And why _wouldn't_ that be an error? You have to identical function
> signatures.

In different scopes.

>
> class X
> {
>      string toString() { return "1"; }
>      string toString() { return "2"; }
> }
>
> isn't legal. What's the difference between that and your mixins?
>
> And can template mixins mixin virtual functions? Normally, templated functions
> can't be virtual (template mixins are instantiated regardless, so they might
> be virtual when mixed into classes - I'm not sure). And if template mixins
> aren't virtual, then _both_ mixins should be illegal, since Object already
> have a virtual toString in it.

template mixins can mixin virtual functions. Just like template classes 
can have virtual functions.

>
> Regardless, I don't see how you could think that your code would be legal.
>

It probably should not be, but this certainly should be legal:
class X{
     mixin T1!() a;
     mixin T2!() b;
     alias a.toString toString;
}

Yet, it yields the same error. The subject seems to be quite moot: If a 
subclass overrides a function declared in multiple named template 
mixins, the first one declared is overridden. I don't really know how it 
should work. The spec is not detailed enough on this. The current 
behaviour is certainly wrong though.



More information about the Digitalmars-d-learn mailing list