digitalmars.D.learn - Error: cannot modify struct arr[0] MyStruct with immutable members
- ref2401 (13/13) May 11 2015 struct MyStruct {
- Adam D. Ruppe (10/11) May 11 2015 You'd just be writing to the immutable memory through a different
- ref2401 (2/13) May 11 2015 Got it. Thank you.
struct MyStruct { this(int a, int b) { this.a = a; this.b = b; } immutable int a; immutable int b; } void main(string[] args) { MyStruct[] arr = new MyStruct[3]; arr[0] = MyStruct(5, 7); } Why does it happen?
May 11 2015
On Monday, 11 May 2015 at 13:37:27 UTC, ref2401 wrote:Why does it happen?You'd just be writing to the immutable memory through a different name. Consider if someone took a reference to one of those immutable ints, expecting it to never change. Then you wrote a new struct over the same location with different values. Then the ints pointed to by that reference suddenly change, despite allegedly being immutable! Assigning a struct in-place is the same as assigning all its members.
May 11 2015
On Monday, 11 May 2015 at 13:44:14 UTC, Adam D. Ruppe wrote:On Monday, 11 May 2015 at 13:37:27 UTC, ref2401 wrote:Got it. Thank you.Why does it happen?You'd just be writing to the immutable memory through a different name. Consider if someone took a reference to one of those immutable ints, expecting it to never change. Then you wrote a new struct over the same location with different values. Then the ints pointed to by that reference suddenly change, despite allegedly being immutable! Assigning a struct in-place is the same as assigning all its members.
May 11 2015