www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Will alias this work for classes too?

reply "Janice Caron" <caron800 googlemail.com> writes:
Will alias this work for classes too?

An in,

class A { int a; }
class B { int b; }
class C { int c; }

class X : A
{
    B b;
    C c;
    alias b this;
    alias c this;
}

Fake multiple inheritance!
Sep 12 2007
next sibling parent davidl <davidl 126.com> writes:
Ah, I haven't noticed such brilliant!

在 Thu, 13 Sep 2007 06:08:48 +0800,Janice Caron <caron800 googlemail.com>  
写道:

 Will alias this work for classes too?

 An in,

 class A { int a; }
 class B { int b; }
 class C { int c; }

 class X : A
 {
     B b;
     C c;
     alias b this;
     alias c this;
 }

 Fake multiple inheritance!
-- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Sep 12 2007
prev sibling parent reply Jascha Wetzel <"[firstname]" mainia.de> writes:
Janice Caron wrote:
 Will alias this work for classes too?
 
 An in,
 
 class A { int a; }
 class B { int b; }
 class C { int c; }
 
 class X : A
 {
     B b;
     C c;
     alias b this;
     alias c this;
 }
 
 Fake multiple inheritance!
that would be a different syntax for regular MI and is therefore probably not going to happen.
Sep 13 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 9/13/07, Jascha Wetzel <"[firstname]" mainia.de> wrote:
 that would be a different syntax for regular MI and is therefore
 probably not going to happen.
"alias this" is already slated in in the Walter Andrei document. Walter apparently /wants/ "alias this", and it looks like it will work as I outlined - at least for structs. Classes? I don't know. That's why I asked. It's not real multiple inheritance. Nobody should pretend it is. But it does "fake it" to a certain extent..
Sep 13 2007
next sibling parent Jascha Wetzel <"[firstname]" mainia.de> writes:
Janice Caron wrote:
 On 9/13/07, Jascha Wetzel <"[firstname]" mainia.de> wrote:
 that would be a different syntax for regular MI and is therefore
 probably not going to happen.
"alias this" is already slated in in the Walter Andrei document. Walter apparently /wants/ "alias this", and it looks like it will work as I outlined - at least for structs. Classes? I don't know. That's why I asked. It's not real multiple inheritance. Nobody should pretend it is. But it does "fake it" to a certain extent..
that depends on what you want to do on name collisions. structs don't have inheritance or virtual functions, which greatly simplifies the problem. once you deal with inheritance and virtual functions as intuitively expected (at least as I intuitively expected), it'll be like real MI.
Sep 13 2007
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
Janice Caron wrote:
 On 9/13/07, Jascha Wetzel <"[firstname]" mainia.de> wrote:
 that would be a different syntax for regular MI and is therefore
 probably not going to happen.
"alias this" is already slated in in the Walter Andrei document. Walter apparently /wants/ "alias this", and it looks like it will work as I outlined - at least for structs. Classes? I don't know. That's why I asked.
If I had to guess, I'd say "alias this" will basically perform a mix-in of the data. So I don't see any reason why it couldn't work for classes.
 It's not real multiple inheritance. Nobody should pretend it is. But
 it does "fake it" to a certain extent..
See this link for an example of mixin multiple inheritance: http://www.invisibleduck.org/~sean/code/io/stream.d It's not an approach I'd want to use regularly, but it can come in handy for eliminating code duplication in some instances. Sean
Sep 13 2007
parent Jascha Wetzel <"[firstname]" mainia.de> writes:
Sean Kelly wrote:
 Janice Caron wrote:
 On 9/13/07, Jascha Wetzel <"[firstname]" mainia.de> wrote:
 that would be a different syntax for regular MI and is therefore
 probably not going to happen.
"alias this" is already slated in in the Walter Andrei document. Walter apparently /wants/ "alias this", and it looks like it will work as I outlined - at least for structs. Classes? I don't know. That's why I asked.
If I had to guess, I'd say "alias this" will basically perform a mix-in of the data. So I don't see any reason why it couldn't work for classes.
mixin merely copies declarations at compile-time. "alias b this" in the sense of the version from Walter's and Andrei's slides, would have to use the reference to the aggregated object to access it's members. a couple of issues come to mind: - while a method that has been mixed in can be overridden in a subclass, a method that has been imported via alias-this can't (unless alias-this would change the vtable of the importing class, which would lead to real MI). - consider this: class A { void foo(int); } class B { void foo(int); } class C : A { override void foo(int); } class D : B { C c; alias C this; } what is D.foo? - the diamond problem of MI also arises - accessing a member (even if it's of primitive type) can result in an access violation although the reference used by the caller is non-null - the aggregated object might be null. sure, these issues can be overcome, but it appears similarly complex as real MI to me.
Sep 13 2007