On Saturday, 18 June 2016 at 02:11:23 UTC, Joerg Joergonson wrote:
I have something like

class X;
class subfoo : X;
class subbaz : X;

class foo : X
{
    subfoo bar;
}

class baz : X;


which I have modified so that

class subbaz : subfoo;
class baz : foo;

(essentially baz is now a derivation of foo while before it was of X)

the problem is that subbaz uses subfoo bar; when it also needs to use a derived type. (so it is a full derivation of foo and subfoo)


To accomplish that I parameterized foo so I can do

class foo!T : X
{
    T bar;
}


and I can now do

class baz : foo!subbaz;


There are two problems with this though:


1. How can I create a default foo!(T = subfoo) so I can just instantiate classes like new foo() and it is the same as foo!subfoo()? I tried creating a class like class foo : foo!subfoo; but I get a collision. I guess an alias will work here just fine though?(just thought of it)

You must declare an alias:

alias FooSubfoo = foo!subfoo;
FooSubfoo fsf = new FooSubfoo;


2. The real problem is that baz isn't really a true derivation of foo like it should be. foo!subfoo and foo!subbaz are different types. I want the compiler to realize that foo!subbaz(and hence baz) is really a derived foo!subfoo and ultimately X.

For multiple inheritence in classes, the standard way of doing is with interfaces.

So instead of a template:

class foo: interfaceThis, interfaceThat
{}


alias this can work too but there only can be one:

class foo!T : X
{
    T bar;
    alias bar this;
}

This pattern is called the "Curiously recurring template" BTW. With the alias this it's almost usable, which was not the case in the original form (because you have an indirection to access the derived type that's indicated by the template parameter. Actually from the machine code POV it's still the case but in the source code it's hidden by the alias this shortcut.





Reply via email to