Bug 1124 – inconsistent: "<" calls opCmp(typeof(this) o); but array.sort calls opCmp(Object o)

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-04-11T14:04:00Z
Last change time
2014-02-16T15:26:08Z
Assigned to
nobody
Creator
someanon

Comments

Comment #0 by someanon — 2007-04-11T14:04:54Z
If a class define both: opCmp(Object o) opCmp(typeof(this) o) "<" calls opCmp(typeof(this) o); but array.sort calls opCmp(Object o) I think we should always call opCmp(typeof(this) o). call opCmp(Object o) only if opCmp(typeof(this) o) is not defined. =================================================================== class A { public: float value; this(float v) {value=v;} int opCmp(Object o) { printf("call opCmp(Object o)\n"); A a = cast(A)o; // ugly cast return this.value < a.value; } int opCmp(typeof(this) o) { printf("call opCmp(typeof(this) o)\n"); return this.value < o.value; } } int main(char[][] args) { int i; A[3] arr; arr[0] = new A(1.0); arr[1] = new A(3.0); arr[2] = new A(2.0); i = arr[0] < arr[1]; printf("%d\n", i); for (i=0; i<3; i++) { printf("%f ", arr[i].value); } printf("\n"); arr.sort; for (i=0; i<3; i++) { printf("%f ", arr[i].value); } printf("\n"); return 0; } ================================================================== $ dmd.exe sortbug.d g:\project\dmd\bin\..\..\dm\bin\link.exe sortbug,,,user32+kernel32/noi; $ ./sortbug.exe call opCmp(typeof(this) o) 0 1.000000 3.000000 2.000000 call opCmp(Object o) call opCmp(Object o) call opCmp(Object o) 3.000000 2.000000 1.000000
Comment #1 by smjg — 2007-09-29T07:31:51Z
That array.sort calls opCmp(Object) is as documented: http://www.digitalmars.com/d/1.0/arrays.html "For the .sort property to work on arrays of class objects, the class definition must define the function: int opCmp(Object). This is used to determine the ordering of the class objects. Note that the parameter is of type Object, not the type of the class." However, this itself seems to be due to Walter's aversion to implementing such features as this using templates. That relational operators call opCmp(typeof(this) o) is a little inaccurate - actually they call opCmp(typeof(that) o). But this is sensible. If both methods exist, then they would have to be equivalent to make sense, likely by this idiom: class Qwert { int opCmp(Object o) { return opCmp(cast(Qwert) o); } int opCmp(Qwert q) { ... } } In this case, why take the performance hit of a runtime cast if it's known at compile time that the RHS is a Qwert? This becomes even more significant if the class is comparable with more than one other class, and you need different code to implement each. Then you'd need something like class Qwert { int opCmp(Object o) { if (cast(Qwert) o) return opCmp(cast(Qwert) o); if (cast(Yuiop) o) return opCmp(cast(Yuiop) o); assert (false); } int opCmp(Qwert q) { ... } int opCmp(Yuiop y) { ... } } The compiler won't necessarily inline the opCmp(Object) call. So cutting out the middleman really is the right thing.
Comment #2 by yebblies — 2012-02-01T07:14:51Z
As Stewart said, this is working as designed.
Comment #3 by bearophile_hugs — 2012-02-05T06:29:18Z
This situation looks bug-prone. But .sort property is going to be deprecated, so I think this problem will solve itself.