Is this an oop-module bug?

Neia Neutuladh neia at ikeran.org
Mon Nov 26 18:44:17 UTC 2018


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.


More information about the Digitalmars-d mailing list