www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - toString multiple overrides

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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(){
}
Feb 10 2012
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Feb 11 2012
prev sibling next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Ellery Newcomer wrote:


 So this behavior is new, but is it sensical?

It is a bug: If a mixin has a MixinIdentifier, it can be used to disambiguate [cited 11 Feb 2012 from http://www.d-programming-language.org/template-mixin.html ]
Feb 11 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-02-11 05:41, 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(){
 }

Shouldn't it be legal since you can qualify the call to toString with the mixin do disambiguate the call? -- /Jacob Carlborg
Feb 11 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 11 Feb 2012 08:56:01 -0500, Timon Gehr <timon.gehr gmx.ch> wrote:

 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.

Mixin identifiers are weird things. Normally, a mixin is like you typed everything the mixin declared in the scope you mix in, but in this case, you are adding some sort of namespace solely for disambiguation. I don't think it makes its own scope. I find the description very confusing in the spec... We have to note two things. First, you want to be able to mixin virtual functions, and second, you want the functions from a mixin to be symbols as if they were declared in the class scope. Given those two things, I don't see how you could possible make the above work. A symbol can't be a.toString, it can only be toString. And since both a.toString and b.toString should be virtual functions, they must occupy the same vtable space. I personally wouldn't mind if mixin identifiers went away, or were replaced with something like a symbol prefix (i.e. the above become a_toString and b_toString). -Steve
Feb 13 2012