Thread overview
Can I set the base class like this?
Jan 26, 2021
Jack
Jan 26, 2021
vitamin
Jan 26, 2021
Jack
Jan 26, 2021
frame
Jan 26, 2021
Jack
January 26, 2021
Can I pass the base class type thought template parameter? something like this:

class MyType { }

class A(T)
{
    void doSomething() { }
}

class B(T)
{
    void doSomething() { }
}

// this class shared stuff to deal with A and B
class C(T) : T!MyType
{
    void doSOmethingElse() { }

    override void doSomething()
    {
        doSOmethingElse();
        super.doSomething();
    }
}

then do something like this:

alias Foo = C!A;
alias Baa = C!B;

instead of:

class Foo : A!MyType
{
    void doSOmethingElse() { }

    override void doSomething()
    {
        doSOmethingElse();
        super.doSomething();
    }
}

class Foo : B!MyType
{
    void doSOmethingElse() { }

    override void doSomething()
    {
        doSOmethingElse();
        super.doSomething();
    }
}

note the body is the same, what changes is the base class. I'd like to avoid repeating myself when the body is the same and only the base class changes.



January 26, 2021
On Tuesday, 26 January 2021 at 04:39:07 UTC, Jack wrote:
> Can I pass the base class type thought template parameter? something like this:
>
> [...]

You have it almost right:

class C(alias T) //....

Template is not type but symbol.
January 26, 2021
On Tuesday, 26 January 2021 at 04:39:07 UTC, Jack wrote:
> note the body is the same, what changes is the base class. I'd like to avoid repeating myself when the body is the same and only the base class changes.

You would have to call it with correct instantiation like

alias Foo = C!(A!bool);

Of course T!MyType would not work but I don't think you want that anyway.

It very depends on the use-case but just use a mixin where you can pass any type you want from template constructor if you don't want to repeat yourself:


class MyType {
}

class A {
}

class B {
}

template base(T) {

    static if (is(T : A)) {
        bool doSomething() {
            return true;
        }
    }
    else static if (is(T : B)) {
        bool doSomething() {
            return false;
        }
    }
    else {
        void doSOmethingElse() {
        }
    }
}

class C(T1, T2) {
    mixin base!T2;

    T1 whatever() {
        return new T1;
    }
}

alias Foo = C!(MyType, A);
alias Baa = C!(MyType, B);


January 26, 2021
On Tuesday, 26 January 2021 at 14:12:21 UTC, vitamin wrote:
> On Tuesday, 26 January 2021 at 04:39:07 UTC, Jack wrote:
>> Can I pass the base class type thought template parameter? something like this:
>>
>> [...]
>
> You have it almost right:
>
> class C(alias T) //....
>
> Template is not type but symbol.

Thanks!
January 26, 2021
On Tuesday, 26 January 2021 at 14:15:25 UTC, frame wrote:
> On Tuesday, 26 January 2021 at 04:39:07 UTC, Jack wrote:
>> note the body is the same, what changes is the base class. I'd like to avoid repeating myself when the body is the same and only the base class changes.
>
> You would have to call it with correct instantiation like
>
> alias Foo = C!(A!bool);
>
> Of course T!MyType would not work but I don't think you want that anyway.
>
> It very depends on the use-case but just use a mixin where you can pass any type you want from template constructor if you don't want to repeat yourself:
>
>
> class MyType {
> }
>
> class A {
> }
>
> class B {
> }
>
> template base(T) {
>
>     static if (is(T : A)) {
>         bool doSomething() {
>             return true;
>         }
>     }
>     else static if (is(T : B)) {
>         bool doSomething() {
>             return false;
>         }
>     }
>     else {
>         void doSOmethingElse() {
>         }
>     }
> }
>
> class C(T1, T2) {
>     mixin base!T2;
>
>     T1 whatever() {
>         return new T1;
>     }
> }
>
> alias Foo = C!(MyType, A);
> alias Baa = C!(MyType, B);

Thank you! I find this approach rather elegant. Ability to pick the method to be part of the class' body without macros is really great.