[Issue 2295] New: automatically covariant types

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 19 10:41:58 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2295

           Summary: automatically covariant types
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: fawzi at gmx.ch


given
{{{
class A{
  A methodA(){
   return this;
  }
}

class B:A{
  B methodB(){
   return this;
  }
}
}}}

call chaining

{{{
B b=new B();
 b.methodA().methodB();
}}}

does not work (also for opCall).

one can make it work by redefining methodA in B, with a call to super and a
cast (or return this).

This use is common enough that hacks to achieve it have been written
(autoOverride in http://team0xf.com:8080/utils/file/9f4c03931278/Meta.d )

Actually if one returns this, one can safely do it for each subclass, and is
widespread enough I think to warrant some language support.

What I would like is to write
{{{
class A{
  This methodA(){
   return this;
  }
}
}}}
or
{{{
class A{
  typeof(this) methodA(){
   return this;
  }
}
}}}
and have the compiler automatically adapt the return type to the actual
(compiletime known) subclass.

It would be equivalent to write 
{{{
  typeof(this) methodA(){ super.methodA(); return this; }
}}}
or
{{{
  typeof(this) methodA(){ return cast(typof(this))super.methodA(); }
}}}
or even
{{{
  typeof(this) methodA(){ union T{typeof(super) s,typeof(this) t}; T
t.s=super.methodA(); return t.t; }
}}}
in each subclass.

The last two methods would allow one to return also null instead of this.


-- 



More information about the Digitalmars-d-bugs mailing list