[Issue 1124] New: inconsistent: "<" calls opCmp(typeof(this) o); but array.sort calls opCmp(Object o)
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Apr 11 12:04:59 PDT 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1124
Summary: inconsistent: "<" calls opCmp(typeof(this) o); but
array.sort calls opCmp(Object o)
Product: D
Version: 1.010
Platform: PC
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: someanon at yahoo.com
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
--
More information about the Digitalmars-d-bugs
mailing list