[Issue 1619] "Missing initializer for const field" only reported if an explicit constructor is present

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 24 07:24:40 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=1619


Maxim Fomin <maxim at maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim at maxim-fomin.ru


--- Comment #2 from Maxim Fomin <maxim at maxim-fomin.ru> 2013-01-24 07:24:35 PST ---
(In reply to comment #1)
> I don't know whether this is a valid report. The same behavior is in D2, afaik
> the field will be initialized with .init, whereas if you introduce a
> constructor the compiler expects you to initialize it yourself.
> 
> Can anyone with more insight confirm?

In both cases i is initialized to 0.

import std.stdio;

class A
{
    const int i;
}

class B
{
    const int i;
    this() { writeln(i); i = 2; }
}

void main()
{
    A a = new A;
    B b = new B;
}

As I understand the point of the requirement is that after exiting from ctor,
const member cannot be altered (this assumption can be invalidated). So, the
only opportunity to make const member store useful runtime value is during
constructor invocation. This ability cannot be replaced by enum or manifest
constant because they are shared across all instances. So, the only opportunity
to configure at rt a const member which can be different across instances is a
ctor (this assumption can be broken). 

However, loosing this opportunity is not an error : if a user forgot to set up
a const member, he can easily go back and modify it. Also a const member is
still initialized, so no invalid value is created. 

When this feature was added it likely had a rationale, but I don't see why not
initializing a const member is treated so severely as an error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list