Tuesday, 15 March 2011

Scala - why fail to override superclass's method -



Scala - why fail to override superclass's method -

class class b extends class d { def get: = ??? } class e extends d { override def get: b = ??? } // ok class f { def set(b: b): unit = ??? } class g extends f { override def set(a: a): unit = ??? } // compile error, override nil

my question why g doesn't work given that: (a=>unit) subtype of (b=>unit)

implicitly[(a => unit) <:< (b => unit)]

the reason simple: jvm doesn't allow overrides based on contravariance on parameter types. allows overrides based on covariance of homecoming type.

take here give-and-take implications of on scala.

from wikipedia:

similarly, type safe allow overriding method take more general argument method in base of operations class:

ex:

class animalshelter { void putanimal(animal animal) { ... } } class catshelter extends animalshelter { void putanimal(object animal) { ... } }

not many object oriented languages allow — c++ , java [and scala] interpret unrelated method overloaded name.

scala

No comments:

Post a Comment