www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bug? Missing initializer...

reply Garett Bass <garettbass studiotekne.com> writes:
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');
}
Jan 16 2006
parent reply Garett Bass <garettbass studiotekne.com> writes:
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);
}
Jan 16 2006
parent Chris Sauls <ibisbasenji gmail.com> writes:
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
Jan 17 2006