Bug 19620 – Not quite sure what immutability means

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-01-26T18:07:13Z
Last change time
2019-01-26T20:17:35Z
Assigned to
No Owner
Creator
Victor Porton

Comments

Comment #0 by porton — 2019-01-26T18:07:13Z
https://dlang.org/spec/const3.html is not quite clear on what immutability means. If a value of an immutable object changes before the immutable-specified var appears, is it legal? Example: char[] a = new char[2]; a[0] = 'x'; a[1] = 'y'; immutable char[] b = cast(immutable char[]) a; // here we have an immutable object which was modified previously Is the above code legal? Need to say it in the specification explicitly.
Comment #1 by dhasenan — 2019-01-26T19:42:58Z
"immutable applies to data that cannot change. Immutable data values, once constructed, remain the same for the duration of the program's execution." It seems extremely clear to me that "cannot change" is not the same as "could never and did never change". "Creating Immutable Data ... The second way is to cast data to immutable. When doing so, it is up to the programmer to ensure that no other mutable references to the same data exist." Since "construct" and "create" are synonyms, it seems rather clear that the spec does not intend to forbid casting previously mutated values to immutable. If you want to make the spec more explicit, please submit a pull request.