Does alias prevent hijacking?
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jun 3 06:23:19 PDT 2011
On Fri, 03 Jun 2011 02:46:23 -0400, Matthew Ong <ongbp at yahoo.com> wrote:
> On 5/30/2011 7:50 PM, Jacob Carlborg wrote:
>> On 2011-05-30 08:53, Kagamin wrote:
>>> class Base
>>> {
>>> void foo(string s){}
>>> }
>>>
>>> class Derived: Base
>>> {
>>> alias Base.foo foo;
>>> void foo(long s){}
>>> }
>>>
>>> So far so good.
>>>
>>> Now add new method to the Base class.
>>>
>>> class Base
>>> {
>>> void foo(string s){}
>>> void foo(int s){}
>>> }
>>>
>>> Now behavior of the calling code silently changes.
>>
>> Yes, but you've made an active choice by adding the alias.
>>
> alias assumes both the Base Class and Derived Class are coded by the
> *same* person and within the *same* module.
>
> What *if* that is *not* the case, I think Kagamin is trying to show that
> *How* would the coder of the Derived be *notify* of this change *even*
> when alias is used.
You can't get this reported. But first, this case is not so common, and
second, an *added* overload to a base class is likely to be similar in
nature to the other overloads. A *new* overload set added to the base
class is unlikely to be related to some similarly named derived class
method.
There is not an easy solution to this, having both foo(int) and foo(long)
can cause trouble in the same overload set.
I agree it is a possible case for hijacking, but I don't think it's common
enough to require more syntax.
-Steve
More information about the Digitalmars-d
mailing list