On Saturday, 7 November 2015 at 11:48:56 UTC, Alex wrote:


So my general question is: why immutable variables shouldn't be able to be moved (inside an array)?

To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes it illegal to assign one instance of ku to another:

void main() {
    ku k = ku(1);
    k = ku(2);
}

That's why it's failing. It's actually possible to use move one instance into another, though:

void main() {
    import std.algorithm : move;
    ku k1 = ku(1);
    ku k2 = ku(2);
    k2.move(k1);
    assert(k1.id == 2);
}

But sort doesn't work that way. You'll need to take a different approach to sort an array of ku.


Reply via email to