<div>Greetings All</div><div><br></div><div>I am trying to compile the following D2 code. The code giving compilation issues is the "this()" function of the class Foo. The constructor basically tries to initialize all the data members of the class, of type BaseClass and of type BaseClass array.</div>
<div><br></div><div>I am using class property tupleof to iterate over members of the class. Then I check the type of each member and if the member is a BaseClass array, I new all the elements of the array. Otherwise if the member is of the type BaseClass, I new it as it is.</div>
<div><br></div><div>The issue is that when I try to compile the program, I get the error</div><div><div>bug.d(10): Error: no property 'length' for type 'test.Bar'</div></div><div><br></div><div>I am explicitly checking the field type, and I am making sure that the field is an array type, before looking for its length. So I am not sure why this error appears. Please guide me.</div>
<div><br></div><div>Regards</div><div>Cherry</div><div><br></div><div>import std.stdio;</div><div>class BaseClass { }</div><div><br></div><div>class Bar: BaseClass { }</div><div><br></div><div>class Foo: BaseClass {</div>
<div>  this() {</div><div>    foreach(i, f; this.tupleof) {</div><div>      if (is (typeof(f) : BaseClass[])) {</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>for (size_t j = 0; j < f.length; ++j) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  f[j] = new typeof(f[j]) ();</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div>      }</div><div>      if (is(typeof(f) : BaseClass)) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>f = new typeof(f) ();</div><div>      }</div><div>    }</div><div>  }</div><div>  Bar instance1;</div><div>  Bar instance2;</div><div>  Bar [10] instances;</div>
<div>}</div><div><br></div><div>unittest {</div><div>  Foo foo;</div><div>  foo = new Foo;</div><div>}</div>