我想尝试编写一个类型,其方法可以是同类的,并返回相同类型的值:
object SimpleTest {
trait Foo extends Product with Serializable {
type Self <: Foo
def bar: Self
}
case class X() extends Foo {
type Self = X
def bar = this
}
case class Y() extends Foo {
type Self = Y
def bar = this
}
trait TC[A]
implicit val tc: TC[Foo] = new TC[Foo] { }
def tester[A: TC](x: Seq[A]) = "foo"
// tester(Seq(X(), Y()))
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,注释掉的行调用tester失败并出现以下错误(Scala 2.10):
Error: could not find implicit value …Run Code Online (Sandbox Code Playgroud)