Thread overview
Bug? Missing initializer...
Jan 17, 2006
Garett Bass
Jan 17, 2006
Garett Bass
Jan 17, 2006
Chris Sauls
January 17, 2006
I was surprised by this error message.  Is this the expected behavior?  If not, I'll post it to bugs, I just wanted a consensus first.

------------
module test;
private import std.stdio;

class Foo {
   const int i;

   this() {
       i = 2;
   }

   this(char c) { // Error: missing initializer for const field i
       this();
   }
}

void main() {
   auto Foo f = new Foo('a');
}
January 17, 2006
Of course, this makes it even worse, since "const" bar is initialized twice.  Basically we can no longer chain constructors in classes with const members to initialize.  I don't know about you, but I use constants pretty frequently, and this seriously impacts the elegance of my constructor code.

Regards,
Garett

------------
module test;
private import std.stdio;

class Foo {
   class Bar { this() { writefln("Bar.this()"); } }

   const auto Bar bar;

   this() {
       bar = new Bar;
   }
   /*
   this(int i) { // missing initializer for const field bar
       this();
       writefln("Foo.this(%d)", i);
   }
   */
   this(float f) { // missing initializer for const field bar
       this();
       bar = new Bar;
       writefln("Foo.this(%0.1f)", f);
   }
}

void main() {
   auto Foo f = new Foo(2.f);
}
January 17, 2006
Garett Bass wrote:
> Of course, this makes it even worse, since "const" bar is initialized twice.  Basically we can no longer chain constructors in classes with const members to initialize.  I don't know about you, but I use constants pretty frequently, and this seriously impacts the elegance of my constructor code.

I do so as well, and I would consider your original post to be symptomatic of a bug.

-- Chris Sauls