November 26, 2018 Is this an oop-module bug? | ||||
---|---|---|---|---|
| ||||
--------------- 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 } } |
November 26, 2018 Re: Is this an oop-module bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gheorghe Gabriel | 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. |
Copyright © 1999-2021 by the D Language Foundation