December 29, 2011 [Issue 7180] New: Documentation bug of "Const and Invariant Structs" | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=7180 Summary: Documentation bug of "Const and Invariant Structs" Product: D Version: D2 Platform: All OS/Version: All Status: NEW Keywords: spec Severity: normal Priority: P2 Component: DMD AssignedTo: andrei@metalanguage.com ReportedBy: k.hara.pg@gmail.com --- Comment #0 from Kenji Hara <k.hara.pg@gmail.com> 2011-12-28 23:48:35 PST --- From http://d-programming-language.org/struct.html > Const and Invariant Structs > > A struct declaration can have a storage class of const, immutable or shared. It has an equivalent effect as declaring each member of the struct as const, immutable or shared. > > const struct S { int a; int b = 2; } > > void main() { > S s = S(3); // initializes s.a to 3 > S t; // initializes t.a to 0 > t = s; // ok, t.a is now 3 // (line 6) > t.a = 4; // error, t.a is const // (line 7) > } Current dmd (2.058head) raises following errors against the sample code: test.d(6): Error: variable test.main.t cannot modify const test.d(7): Error: can only initialize const member a inside constructor Because the definition of S is internally translated to: struct __S { int a; int b = 2; } alias const(__S) S; But, if you replace the definition to struct S { const int a; const int b = 2; } the compilation still raises: test.d(6): Error: variable test.main.t cannot modify struct with immutable members test.d(7): Error: can only initialize const member a inside constructor Because mutable object that has non-mutable members is "not assignable", then t = s is invalid, even if t and s are mutable S. Finally I think this is documentation bug. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 21, 2012 [Issue 7180] Documentation bug of "Const and Invariant Structs" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | http://d.puremagic.com/issues/show_bug.cgi?id=7180 Walter Bright <bugzilla@digitalmars.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |bugzilla@digitalmars.com Resolution| |WORKSFORME --- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2012-01-21 01:18:28 PST --- The documentation was recently fixed. -- 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