Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contravariant method argument type

wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that.

example:

class AnimalShelter {
    Animal getAnimalForAdoption() {      ...        }         
    void putAnimal(Animal animal) {      ...        }   
}

class CatShelter extends AnimalShelter {
    @Overriding        
    Cat getAnimalForAdoption() {  return new Cat();    }        
    @Overriding                    
    void putAnimal(Object animal) {      …        }     
}

My question is:

  1. Is contravariant argument type of overriding method any of good use? if yes, where it is?
  2. Is method a function? Why Scala has different rule for function type and overriding method type?
like image 382
WeiChing 林煒清 Avatar asked Dec 12 '22 06:12

WeiChing 林煒清


1 Answers

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.

like image 143
Alexey Romanov Avatar answered Dec 23 '22 19:12

Alexey Romanov