Is contravariant argument type of overriding method any of good use? if yes, where it is?
Example translated from the Sather documentation:
interface Carnivore {
void eat(Meat food);
}
interface Herbivore {
void eat(Plant food);
}
interface Omnivore extends Carnivore, Herbivore {
// overrides both above eat methods,
// since Meat and Plant are subtypes of Food
void eat(Food food);
}
Is method a function?
In Scala? No, but it can be converted to a function.
Why Scala has different rule for function type and overriding method type?
Because overriding method types has to follow JVM's rules. It could be done by creating bridge methods (in the case above, adding methods eat(Plant)
and eat(Meat)
which just call eat(Food)
), similarly to the way covariant return type is implemented, but it would add complexity to the language without much benefit.