Could new keyword help function hijacking and prevented aliasing all over the place??

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri May 27 09:40:27 PDT 2011


On 5/27/11 10:04 AM, Steven Schveighoffer wrote:
> On Fri, 27 May 2011 10:54:14 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> It is completely against the spirit of the language to decide that a
>> call is resolved to an invalid method during runtime. There is no
>> other feature remotely related to hiddenfunc.
>>
>> A couple of years ago, Walter gave a talk on hijacking to NWCPP. It
>> all went well until HiddenFunc, at which point Walter's assertion that
>> the way out was by throwing an exception was hotly debated. Several
>> people suggested alternative, of whom one proposed (4) above.
>> Everybody agreed it's a good solution, and Walter had the presence of
>> mind and humility to acknowledge that solution and to promise to look
>> into implementing it. Unfortunately, that event was forgotten... until
>> now.
>
> I just tried it out. If one implements an overload that is not
> contentious (for example, between int and string), no hidden func error
> is thrown. So indeed the compiler has a notion of when a function would
> be hijacked.
>
> I thought HiddenFuncError was thrown whenever you called any overloaded
> base method. So I agree with you, this needs to be fixed. Is there a
> bugzilla on it, or should we file one? Let's not lose it again.
>
> -Steve

Matthew's example fixed is this:

class Parent{
     void methodA(int x){}
}

class Child : Parent{
     //alias Parent.methodA methodA;
     void methodA(long x){}
}

void main(string[]){
     Parent obj = new Child();
     obj.methodA(1);
}

The program throws with the alias commented out, doesn't throw with the 
alias. The desired behavior is to enforce three things: (a) "override" 
must be required everywhere overriding takes place (no warning, no debug 
mode etc); (b) if a class introduces a method with parameter types 
covariant with those of a homonym base method, it must introduce all 
overloads of that name (via alias or overriding); (c) if a class 
introduces two overloaded AND overridable methods, those must NOT have 
covariant parameters.

(a) makes sure no undue overriding is ever present, (b) takes care of 
this example, and (c) takes care of the HiddenFunc examples given online 
at http://www.digitalmars.com/d/2.0/function.html.

If you find the time, please make a bug report out of it and insert a 
link to this discussion in the report.


Thanks much,

Andrei


More information about the Digitalmars-d mailing list