digitalmars.D.learn - About Immutable struct members and arrays.
- Jack Applegame (14/14) Jan 06 2016 import std.algorithm;
- anonymous (13/26) Jan 06 2016 Assuming you meant `arr[0].b = 4;`. Just overwriting the mutable part of...
- Jack Applegame (5/10) Jan 07 2016 I think it isn't a bug. I'm not overwriting immutable data, I'm
- anonymous (4/7) Jan 07 2016 I don't think it's move's purpose to ignore the type system like that.
import std.algorithm; struct Bar { const int a; int b; } void main() { Bar[1] arr; Bar bar = Bar(1, 2); bar[0].b = 4; move(bar, arr[0]); // ok arr[1] = bar; // fail, why? move(Bar(1, 2), arr[0]); // fail, why source parameter isn't auto ref? }
Jan 06 2016
On 06.01.2016 23:04, Jack Applegame wrote:import std.algorithm; struct Bar { const int a; int b; } void main() { Bar[1] arr; Bar bar = Bar(1, 2); bar[0].b = 4;Assuming you meant `arr[0].b = 4;`. Just overwriting the mutable part of bar[0] is ok, of course.move(bar, arr[0]); // okI consider it a bug that this compiles. You're overwriting immutable data, which shouldn't be possible (without casting). https://issues.dlang.org/show_bug.cgi?id=15315arr[1] = bar; // fail, why?Assuming you meant `arr[0] = bar;`. The error message isn't too bad here: "Error: cannot modify struct arr[0] Bar with immutable members". You're trying to overwrite immutable data, that's not allowed.move(Bar(1, 2), arr[0]); // fail, why source parameter isn't auto ref?I'm not sure about the details. Maybe it would make sense for `move` to accept rvalues, maybe not. Breaking immutable is certainly not a good reason to do it, though.}
Jan 06 2016
On Thursday, 7 January 2016 at 00:19:12 UTC, anonymous wrote:On 06.01.2016 23:04, Jack Applegame wrote:I think it isn't a bug. I'm not overwriting immutable data, I'm replacing old data with a new constructed one. I want to destruct old data and fill uninitialized raw memory with a new constructed one.move(bar, arr[0]); // okI consider it a bug that this compiles. You're overwriting immutable data, which shouldn't be possible (without casting). https://issues.dlang.org/show_bug.cgi?id=15315
Jan 07 2016
On 07.01.2016 09:16, Jack Applegame wrote:I think it isn't a bug. I'm not overwriting immutable data, I'm replacing old data with a new constructed one. I want to destruct old data and fill uninitialized raw memory with a new constructed one.I don't think it's move's purpose to ignore the type system like that. If it was, it would need more warning signs and preferably a more obscure name.
Jan 07 2016