www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is this an oop-module bug?

reply Gheorghe Gabriel <knoppy273 live.com> writes:
--------------- module 1 ---------------
abstract class Base {
     protected final typeof(this) a() { return this; }
     protected final typeof(this) b() { return this; }
}

--------------- module 2 ---------------
final class Class : Base {
     this() {
         a(); // ok
         b(); // ok

         a().b(); // error, b() call is not accessible from this 
module
     }
}
Nov 26 2018
parent Neia Neutuladh <neia ikeran.org> writes:
On Mon, 26 Nov 2018 18:25:11 +0000, Gheorghe Gabriel wrote:
 abstract class Base {
      protected final typeof(this) a() { return this; }
      protected final typeof(this) b() { return this; }
 }
typeof(this) is Base. In any derived class, it will still be Base.
 final class Class : Base {
      this() {
          a().b(); // error, b() call is not accessible from this
 module
      }
 }
The spec says:
 If accessing a protected instance member through a derived class member
 function, that member can only be accessed for the object instance which
 can be implicitly cast to the same type as ‘this’.
a() returns a Base. Base cannot be implicitly cast to Class. If you need this to work, you can write: abstract class Base(T) { protected T a() { return cast(T)this; } protected T b() { return cast(T)this; } } // in another module: class Derived: Base!Derived { this() { a().b(); } } There are a couple other ways to do this.
Nov 26 2018