Thread overview | |||||
---|---|---|---|---|---|
|
October 27, 2007 [Issue 1619] New: "Missing initializer for const field" only reported if an explicit constructor is present | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=1619 Summary: "Missing initializer for const field" only reported if an explicit constructor is present Product: D Version: 1.022 Platform: PC OS/Version: Windows Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: bugzilla@digitalmars.com ReportedBy: ary@esperanto.org.ar The following code gives a compiler error: --- class X { const int x; this() { } } --- main.d(3): constructor main.X.this missing initializer for const field x But this one: --- class X { const int x; } --- compiles correctly. Since the semantic of both files is the same, an error should also be reported in the second case. -- |
January 23, 2013 [Issue 1619] "Missing initializer for const field" only reported if an explicit constructor is present | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1619 Andrej Mitrovic <andrej.mitrovich@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |andrej.mitrovich@gmail.com --- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-23 11:08:02 PST --- 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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 24, 2013 [Issue 1619] "Missing initializer for const field" only reported if an explicit constructor is present | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1619 Maxim Fomin <maxim@maxim-fomin.ru> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |maxim@maxim-fomin.ru --- Comment #2 from Maxim Fomin <maxim@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: ------- |
Copyright © 1999-2021 by the D Language Foundation