sorting a mutable array of immutable objects
Jeff Thompson via Digitalmars-d
digitalmars-d at puremagic.com
Wed Apr 20 03:02:03 PDT 2016
On Wednesday, 20 April 2016 at 09:54:13 UTC, Jeff Thompson wrote:
> 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""
> }
Actually, maybe my problem is that the opCmp override should take
a const Object (for comparing with an immutable object). Is there
a way to do that?
More information about the Digitalmars-d
mailing list