Am I getting this all right?

Ary Manzana ary at esperanto.org.ar
Wed Dec 13 17:13:10 PST 2006


Sean Kelly escribió:
> Jason House wrote:
>> I couldn't find a way to reinitialize a variable back to a default 
>> value...
>> For example class foo(T){ T[] bar; void func(){bar[3] = new T();} } won't
>> compile.  I'm not too sure why this is the case.
> 
> Try:
> 
>     T[] bar = new T[size];
>     T[0 .. $] = new T();

It worked for me, if T is a class. If it's a primitive type, you should 
do "T.init" instead of "new T()". So heres the code:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
class foo(T) {
	
	T[] bar;
	
	void func(){
		static if (is(T == class)) {
			bar[3] = new T();
		// TODO: what if it's a struct, typedef, etc.?
		} else {
			bar[3] = T.init;
		}
	}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

BTW, I could compile this:

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
class foo(T) {
	
}

void main() {
	mixin foo!(int) a;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

but this gives me an error

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
1. class foo(T) {
2. 	
3. }
4.
5. void main() {
6.	mixin foo!(int) a;
7.	mixin foo!(real) b;
8. }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 > main.d(7): mixin foo!(real) foo is not a template

Why?


More information about the Digitalmars-d-learn mailing list