opCmp with structs

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 7 06:36:22 PST 2015


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.




More information about the Digitalmars-d-learn mailing list