> if(false) {
>    for (size_t j = 0; j < f.length...)
>    ...
> }
>
> Semantically this code is wrong as you can't take the length of f which is 
> class Bar. The static if forces the compiler to not generate this code as it 
> is known to be false.
>

Thanks Jesse

What you are saying makes sense to me. The problem is that the
following code works perfectly. I have just commented out some part
and replaced it with some debug statements.

The output from this code makes me believe that "is" and "typeof" do
have some run-time semantics. Or is it a D2 bug. BTW I am using
Digital Mars D Compiler v2.050.

Regards
- Puneet

import std.stdio;
class BaseClass { }

class Bar: BaseClass { }

class Foo: BaseClass {
  this() {
    foreach(i, f; this.tupleof) {
      if (is (typeof(this.tupleof[i]) : BaseClass[])) {
        writeln("Creating new objects for all ARRAY types ", 
this.tupleof[i].stringof);
        // for (size_t j = 0; j < this.tupleof[i].length; ++j) {
        //   this.tupleof[i][j] = new typeof(this.tupleof[i][j]) ();
        // }
      }
      if (is(typeof(this.tupleof[i]) : BaseClass)) {
        writeln("Creating new objects for all NON-ARRAY types ",
this.tupleof[i].stringof);
        // this.tupleof[i] = new typeof(this.tupleof[i]) ();
      }
    }
  }
  Bar instance1;
  Bar instance2;
  Bar [10] instances;
}

unittest {
  Foo foo;
  foo = new Foo;
}

// I am getting the following output
// Creating new objects for all NON-ARRAY types this.instance1
// Creating new objects for all NON-ARRAY types this.instance2
// Creating new objects for all ARRAY types this.instances

Reply via email to