I have identified the symptom:
trait A {
def hello(name:Any):Any
}
Then define the class X to implement it:
class X extends A {
def hello(name:Any): Any = {}
}
Compiled. Then I change the return type in the subclass:
class X extends A {
def hello(name:Any): String = "hello"
}
It is also compiled. Then change the parameter type:
class X extends A {
def hello(name:String): Any = {}
}
It cannot compile this time, error:
error: class X needs to be abstract, since method hello in trait A of type (name: Any)
Any is not defined
(Note that Any does not match String: class String in package lang is a subclass
of class Any in package scala, but method parameter types must match exactly.)
It seems that the parameter should exactly match, but the return type may be a subtype in the subclass?
Update: @ Mik378, thanks for your answer, but why can't the following example work? I think this will not break Liskov:
trait A {
def hello(name:String):Any
}
class X extends A {
def hello(name:Any): Any = {}
}
source
share