Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there statically-typed programming-languages with inheritance where method parameters are contravariant?

Theoretically it is sound to override methods of a parent in a subclass with a method where the parameters are supertypes of the parameters in the parent class, like:

class T
  def foo(s: String) = ...

class S
  override def foo(a: Any) = ...

Which programming languages support this "feature" and how do they solve issues like the possibility that a single method of a subclass can override multiple methods of a parent class:

class T
  def foo(s: String) = ...
  def foo(i: Int)    = ...

class S
  override def foo(a: Any) = ...

(Assuming that String and Int subtypes of Any.)

like image 665
soc Avatar asked Nov 04 '22 11:11

soc


1 Answers

(Incomplete answer) If we take the example of OCaml, it answers the title of your question: this a "statically-typed programming language with inheritance where method parameters are contravariant".

As for the second part of your question ("how do they solve issues like..."), the Gordian knot is cut by not allowing overloading. (Overloading means having two functions with the same name but different argument types, which makes type inference a lot harder).

like image 51
jrouquie Avatar answered Nov 11 '22 13:11

jrouquie