Facing problems with Class Properties

d coder dlang.coder at gmail.com
Fri Dec 10 08:35:50 PST 2010


Greetings All

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.

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.

The issue is that when I try to compile the program, I get the error
bug.d(10): Error: no property 'length' for type 'test.Bar'

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.

Regards
Cherry

import std.stdio;
class BaseClass { }

class Bar: BaseClass { }

class Foo: BaseClass {
  this() {
    foreach(i, f; this.tupleof) {
      if (is (typeof(f) : BaseClass[])) {
for (size_t j = 0; j < f.length; ++j) {
  f[j] = new typeof(f[j]) ();
}
      }
      if (is(typeof(f) : BaseClass)) {
f = new typeof(f) ();
      }
    }
  }
  Bar instance1;
  Bar instance2;
  Bar [10] instances;
}

unittest {
  Foo foo;
  foo = new Foo;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20101210/92fe747f/attachment.html>


More information about the Digitalmars-d-learn mailing list