Rebind template(bug?)

Engine Machine via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 21 17:43:00 PDT 2016


On Monday, 22 August 2016 at 00:22:48 UTC, ag0aep6g wrote:
> On 08/22/2016 12:06 AM, Engine Machine wrote:
>> T!()'s "data" is specified in the class just like all the other
>> derivations. I don't want to have to specify an external base 
>> class as
>> in your InstaniateOrBase. Why? Because!!! (There should be no 
>> need to,
>> and if one goes this route of creating classes, it should be 
>> all or
>> nothing, else there is no real benefit)
>
> You make it sound like I came up with the external base class, 
> but I just took that from your code.
>
No, what I meant was I don't want a base. I only added it in my 
code so it would compile/do what I want.

> [...]
>> It seems that when one uses `class T(A...) : X`, one can't, 
>> for some X,
>> not have inheritance. The compiler expects X to be something 
>> inheritable
>> from no matter what.
>
> I didn't know it, but your code shows that X can also be an 
> empty compile time list (Seq). I have no idea if this is in the 
> spec, or if it just happens to work with dmd.
>
> If it's valid D, you just need a template that goes from T and 
> A to T!(A[0 .. $ - 1]), or if A is already empty, to the empty 
> Seq.
>
> Like so:
>
> ----
> 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)
> {
>     ...
> }
> ----

Yes! I though I tried that from Timon's original solution.

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);
}

The only down side is that the static if's are flat. They don't 
show the relationship between, say, Dog an Animal, that a nested 
set of static if's would show, but I think this is not possible 
due to the recursion and shadowing(I imagine one could test if 
the variables exist but that is messy).

Anyways, Thanks for the help.

I think this is just Timons answer anyways. What the original 
confusion was, was the shadowing, which I thought would not 
happen(or at least expected an error), Lodovico solved that.






More information about the Digitalmars-d-learn mailing list