www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I set the base class like this?

reply Jack <jckj33 gmail.com> writes:
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.
Jan 25 2021
next sibling parent reply vitamin <vit vit.vit> writes:
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.
Jan 26 2021
parent Jack <jckj33 gmail.com> writes:
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!
Jan 26 2021
prev sibling parent reply frame <frame86 live.com> writes:
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);
Jan 26 2021
parent Jack <jckj33 gmail.com> writes:
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.
Jan 26 2021