(D1) Mixing in constructors

Justin Whear justin at economicmodeling.com
Fri Nov 6 09:47:59 PST 2009


I've been trying to use a template to mixin constructors and running into what seems like a bug. Here's the template:
public template ChainNodeSubclassing {
	this() {
		super();
	}
	this(string id) {
		super(id);
	}

...some other stuff...
}

Using this, compilation would always fail and complain that Object has no super class. This is strange because template mixins are supposed to only be evaluated where they're mixed-in, namely in the body of my classes which certainly do have super-classes.

So I tried getting around it like this (just the second constructor for brevity):
this(string id) {
	static if (!is(typeof(this) : Object))
	{
		super(id);
		writefln("Instantiating %s with ID", this.classinfo.name);
	} else {
		throw new Exception("Cannot instantiate type");
	}
}

Now it compiles, but I always get errors at runtime when I try to create of one the classes with this mixin. It seems that every object is implicitly convertible to Object. Is this a bug or am I doing something really wrong?


More information about the Digitalmars-d-learn mailing list