sorting a mutable array of immutable objects

Jeff Thompson via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 20 02:54:13 PDT 2016


I can create a mutable array of immutable objects, thanks to the 
answers to my other question 
https://forum.dlang.org/post/nf57f5$e6v$1@digitalmars.com .

Now I want to sort the array, but the sort function doesn't "see" 
the opCmp in class C. Do I need to define a custom compare 
function for each class where I want to use Rebindable? Or can 
Rebindable itself be updated to "pass through" methods like opCmp?

class C {
   this(int x) immutable { this.x = x; }
   override int opCmp(Object o) const { return x - (cast(C)o).x; }
   int x;
}

void main(string[] args)
{
   auto array = new Rebindable!(immutable(C))[2];
   array[0] = new immutable C(20);
   array[1] = new immutable C(10);
   sort(array); // Error: "Invalid predicate passed to sort: "a < 
b""
}



More information about the Digitalmars-d mailing list