toString multiple overrides

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 10 23:48:05 PST 2012


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.

class X
{
    string toString() { return "1"; }
    string toString() { r eturn "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.

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

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list