On Thursday, 24 May 2018 at 20:24:32 UTC, IntegratedDimensions wrote:
class T;
class TT : T;

interface I
{
   @property T t();
}

abstract class A
{
   T _t;
   @property T t() { return _t; }

}

class C : A
{

// Stuff below uses t as TT but compiler, of course, treats t as T
   ...
}


The issue is that I programmed the class C with a variable that directly was based off TT, I later subderived T from TT and exposed it in I. (TT was refactored in to T and not T)


As as a side note:
I can hardly follow this, as you don't show, where you use the interface I. However, especially if TT was refactored in such a way, that is a set difference of T and not T, why you choose to derive from T instead of to contain T?

https://en.wikipedia.org/wiki/Composition_over_inheritance
http://wiki.c2.com/?CompositionInsteadOfInheritance

Well, can imagine useful cases though...


But all the code in C assumes t is of type TT but now due to the interface it looks like a T, even though internally it is actually a TT.

What I'd like to do is

class C : A
{
private override @property TT t() { return cast(TT)(_t); } // null check if necessary
   // Stuff below uses t which is now a TT
   ...
}

or whatever.

This is simply so I don't have to rename or cast all my uses of t in C to type TT.

I'm pretty much guaranteed that in C, t will be type TT due to the design(C goes with TT like bread with butter).

So, it would be nice if somehow I could inform the type system that in C, t is always of type TT and so treat it as such rather than forcing me to explicitly cast for every use. Again, I could rename things to avoid the same name usage but in this case it is not necessary because of the design.

Is there any semantics that can get me around having to rename?

Maybe, you are looking for Curiously Recurring Template Pattern?

´´´
interface I(P)
{
        @property P t();
}

abstract class T(P) : I!P
{
    P _p;
    @property P t() { return _p; }
}

class TT : T!TT
{

}

void main()
{
        auto tt = new TT();
        static assert(is(typeof(tt.t) == TT));
}
´´´

Reply via email to