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

%u z at gg.com
Mon Sep 11 18:16:36 PDT 2006


==========================================================================

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

==========================================================================

This is realy ugly.

For these special method (.dup, .opCmp, .equals), Why can't we have coveriant
parameters, or even anchored types as in eiffel?

http://www.pi.informatik.tu-darmstadt.de/inf1/eiff-ref/chap12.htm

So we can write more clean code:

class Dog : Animal {

Dog dup()           {...}
int opCmp(Dog d)    {...}
int opEquals(Dog d) {...}

}

Instead of casting Object around?




More information about the Digitalmars-d-bugs mailing list