On Monday, 22 August 2016 at 07:54:36 UTC, Jack Applegame wrote:
On Monday, 22 August 2016 at 00:43:00 UTC, Engine Machine wrote:
The following code works and does what I want!
template InstantiateOrEmptySeq(alias tmpl, args...)
{
alias Seq(T...)=T;
static if (args.length > 0)
alias InstantiateOrEmptySeq = tmpl!(args[0 .. $-1]);
else
alias InstantiateOrEmptySeq = Seq!();
}
class T(A...) : InstantiateOrEmptySeq!(T, A)
{
static if (A.length == 0)
{
// Base class
int x;
} else
static if (A[$-1] == "Animal")
{
int y;
} else
static if (A[$-1] == "Dog")
{
int z;
} else
static if (A[$-1] == "Pug")
{
int s;
} else static assert(A[$-1]~" not a defined class of
"~this.stringof);
}
Why don't you like a cleaner (in my opinion) solution?
class T() {
// Base class
int x;
}
class T(A...) : T!(A[0..$-1]) {
static if (A[$-1] == "Animal")
{
int y;
} else
static if (A[$-1] == "Dog")
{
int z;
} else
static if (A[$-1] == "Pug")
{
int s;
} else static assert(A[$-1]~" not a defined class of
"~this.stringof);
}
How do you seriously think this is cleaner/simpler? You have two
classes. Their is no uniformity between them. You have uniformity
between all the derived classes then have a special case for the
base class. A certain easy to follow pattern is set up but
needlessly break it for one case. Why?