match class specialization

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Apr 25 10:53:25 PDT 2007


mandel wrote:
> Well, you code works fine when foo is A!(T) no matter what T is,
> but it doesn't work when foo inherits from A!(T).
> So, the problem still remains..

The code provided doesn't compile on GDC ("Error: identifier 'T' is not 
defined")[1]


Therefore, the following is for DMD only:
To walk up the inheritance tree:
-----
import std.stdio;

class A(T) {}

class B(T) : A!(T) {}

template TestA(U : A!(T))
{
         const TestA = true;
}

template TestA(U)
{
         static if (is(U Bases == super) && is(Bases[0] == class))
		const TestA = TestA!(Bases[0]);
	else
		const TestA = false;
}

void test(U)() {
	writef(typeid(U));
	static if(TestA!(U)) {
		writefln("\t: A(T)");
	} else {
		writefln("\t: not A(T)");
	}
}

class C {}

void main()
{
	test!(B!(int));
	test!(A!(int));
	test!(int);
	test!(C);
}
-----

Assumptions:
* A!(whatever) is a class, not an interface.
* The first element of the tuple 'returned' by "is(X Tuple == super)" is 
the base class, if any. I'm not sure if this is guaranteed, but it seems 
to be the case anyway. If you want to be sure, modify the test to 
recurse over all base classes.

Also note that like the original, it doesn't compile with GDC.



[1]: Does anyone know if that's
a) a bug in GDC,
b) a bug in DMD,
c) a consequence of GDC being at DMD v1.007 instead of v1.013, or
d) something else?

I don't feel like digging into the spec to find out what, if anything, 
it says about this.


More information about the Digitalmars-d-learn mailing list