[Issue 323] New: Error: need opCmp for class Bar

Thomas Kuehne thomas-dloop at kuehne.cn
Thu Sep 7 01:41:41 PDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail at puremagic.com schrieb am 2006-09-03:
> http://d.puremagic.com/issues/show_bug.cgi?id=323

> I think this use to work; now on Linux dmd.166, it reports:

http://www.digitalmars.com/d/changelog.html#new0163
#
# Object.opCmp now throws an error. Should override it
# if used. Breaks existing code.
#

> Error: need opCmp for class Bar
>

<snip>
>   int opCmp(T other) {
>     int r;
>
>     if      (value < other.value)  {r = -1;}
>     else if (value > other.value)  {r =  1;}
>
>     return r;
>   }

The cause is a nasty result of D's function inheritance and overriding.

http://www.digitalmars.com/d/function.html
#
# A functions in a derived class with the same name and parameter types
# as a function in a base class overrides that function.
#

Thus "int T.opCmp(T other)" dosen't override "int Object.opCmp(Object o)" ...

Should do the trick:
#
# int opCmp(Object o){
#    T t = cast(T) o;
#    if(t is null){
#       return super.opCmp(o);
#    }else{
#       return opCmp(t);
#    }
# }
#
#   int opCmp(T other) {
#     int r;
#
#     if      (value < other.value)  {r = -1;}
#     else if (value > other.value)  {r =  1;}
#
#     return r;
#   }
#

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFE/+hrLK5blCcjpWoRAn19AJ4xroQQhMNn1DkxLCAafdrzXw2UswCeNZfA
2eWscmpgVCkItONOreZGRmo=
=UXBJ
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-bugs mailing list