Inherit from class based on bool value

Basile B. b2.temp at gmx.com
Tue Nov 13 11:41:50 UTC 2018


On Tuesday, 13 November 2018 at 07:10:26 UTC, Jamie wrote:
> I would like my class to inherit from one of two classes based 
> on a boolean value known at compile time. Something like this:
>
> void main()
> {
>     Top!(OPTION.FALSE) top = new Top!(OPTION.FALSE);
> }
>
> enum OPTION
> {
>     FALSE = 0.,
>     TRUE = 1.
> }
>
> class One
> {}
>
> class Two
> {}
>
> class Top(OPTION option) : option ? One : Two
> {}
>
> Is this possible? I can't get it to work in the way I'm showing 
> above.
> Cheers

You can use an alias seq :

void main()
{
     Top!(OPTION.FALSE) top = new Top!(OPTION.FALSE);
}

enum OPTION
{
     FALSE = 0,
     TRUE = 1
}

class One
{}

class Two
{}

import std.meta : AliasSeq;
alias Bases = AliasSeq!(One, Two);

class Top(OPTION option) : Bases[option]
{}


More information about the Digitalmars-d-learn mailing list