toString multiple overrides

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 13 06:44:08 PST 2012


On Sat, 11 Feb 2012 08:56:01 -0500, Timon Gehr <timon.gehr at 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


More information about the Digitalmars-d-learn mailing list