www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does alias prevent hijacking?

reply Kagamin <spam here.lot> writes:
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.
May 29 2011
parent reply Jacob Carlborg <doob me.com> writes:
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. -- /Jacob Carlborg
May 30 2011
parent reply Matthew Ong <ongbp yahoo.com> writes:
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. -- Matthew Ong email: ongbp yahoo.com
Jun 02 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 03 Jun 2011 02:46:23 -0400, Matthew Ong <ongbp 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
Jun 03 2011
parent Matthew Ong <ongbp yahoo.com> writes:
On 6/3/2011 9:23 PM, Steven Schveighoffer wrote:
 *How* would the coder of the Derived be *notify* of this change *even*
 when alias is used.
I was *not* referring to my earlier suggestions about new pair of keywords at all. Unless that solved this issue, but it does NOT. I am trying to show that is what he is trying to ask: *How* would the coder of the Derived be *notify* of this change *even* when alias is used. I begin to see why hijacking is a problem in a inheritance tree, more clearly with this example posted by Kagamin. If someone messes the top of the tree, that is going to be a big problem. In javac there is such warning. But, this example, there does not seems to have one for D.
I agree it is a possible case for hijacking, but I don't think it's 
common enough to require more syntax. May I ask, if there is any factual statistical calculations done or just purely manual 'observational'. -- Matthew Ong email: ongbp yahoo.com
Jun 04 2011