Thread overview
does array.sort work at all for opCmp?
Aug 11, 2005
z
Aug 11, 2005
zwang
Aug 11, 2005
Niko Korhonen
Aug 11, 2005
z
August 11, 2005
dmd 129:

sortbug.exe
1.000000 3.000000 2.000000
2.000000 3.000000 1.000000


sortbug.d
------------------------------------------
class A {
public:
float value;

this(float v) {value=v;}

int opCmp(A o) {
if (this.value < o.value)  return -1;
if (this.value > o.value)  return  1;
return 0;
}

}
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);
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;
}
------------------------------------------


August 11, 2005
You need to define int opCmp(Object o){ ... }

z@gg.com wrote:
> dmd 129:
> 
> sortbug.exe
> 1.000000 3.000000 2.000000
> 2.000000 3.000000 1.000000
> 
> 
> sortbug.d
> ------------------------------------------
> class A {
> public:
> float value;
> 
> this(float v) {value=v;}
> 
> int opCmp(A o) {
> if (this.value < o.value)  return -1;
> if (this.value > o.value)  return  1;
> return 0;
> }
> 
> }
> 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);
> 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;
> }
> ------------------------------------------
> 
> 
August 11, 2005
zwang wrote:
> You need to define int opCmp(Object o){ ... }

...and I think it should be a compiler warning trying to define only opCmp(MyObj) without defining opCmp(Object) for objects. It would save us from so much hassle.

The same thing for not defining toHash when defininng opEquals and so forth.

-- 
Niko Korhonen
SW Developer
August 11, 2005
>zwang wrote:
>> You need to define int opCmp(Object o){ ... }

Does D support covariant argument type for class method?