Hijacking

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Aug 11 11:33:42 PDT 2007


Regan Heath wrote:
> Walter Bright wrote:
>> Kristian Kilpi wrote:
>>>
>>> The old aliasing trick will still cause problems.
>>>
>>> (The 'override' keyword won't solve the problem in the following case.)
>>>
>>>   class A {
>>>     void foo(int);
>>>     void foo(long);
>>>   }
>>>
>>>   class B : A {
>>>     alias A.foo foo;
>>>     override void foo(int);
>>>   }
>>>
>>>   void bar(B b) {
>>>     short val = 1;
>>>
>>>     b.foo(val);  //calls 'foo(int)'
>>>   }
>>>
>>> Now 'foo(short)' is added to 'A':
>>>
>>>   void bar(B b) {
>>>     short val = 1;
>>>
>>>     b.foo(val);  //calls 'foo(short)'
>>>   }
>>
>> I don't think this is a problem, because the B author explicitly and 
>> deliberately opened the door to this, therefore he is taking 
>> responsibility for it.
> 
> Yes and no.
> 
> At the time the author wrote "alias A.foo foo;" they were allowing void 
> foo(long), not void foo(short).
> 
> Yes, they still "opened the door to this" because D's current behaviour 
> is to pull all overloads past/present/future.
> 
> Perhaps being able to specify overloads exactly would in some cases be 
> of benefit, eg.
> 
> alias A.foo(long) foo;
> 
> Regan

Honestly, we need some ability to specify more of a function/method's signature in the 
general case anyhow.  (Return type isn't really important, since it can't be different 
without at least one parameter difference)  This would help with the super-aliasing case 
above, /and/ with the address operator.

void foo (int);
void foo (char[]);

Currently:
&foo => *(foo(int))

Proposed:
&foo         => *(foo(int)) or ambiguity error?
&foo(int)    => *(foo(int))
&foo(char[]) => *(foo(char[]))

Currently:
alias foo bar; // all foo's

Proposed:
alias foo         bar; // all foo's
alias foo(int)    bar; // just this one
alias foo(char[]) bar; // ditto

The only thing that might get interesting is whether the compiler can easily distinguish 
(&foo()) as "take the address of a function foo with no params" versus "take the address 
of the result of function foo".  (Is that even legal?  Or useful?)

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list