1

我如何让它编译?

代码

object Playground2 {

  trait Client[S,A] {
    def wrap[S,A](v: A): (S,A)
  }

  class TestClient extends Client[String, Int] {
    override def wrap[String,Int](v: Int): (String, Int) = ("cache 2.00", v)
  }
}

错误 类型不匹配;发现:java.lang.String("cache 2.00") required: String

4

1 回答 1

2

这是编译的代码版本:

object Playground2 {

  trait Client[S,A] {
    def wrap(v: A): (S,A)
  }

  class TestClient extends Client[String, Int] {
    override def wrap(v: Int) = ("cache 2.00", v)
  }
}

您在函数中再次复制了类型,wrap并且不需要,因为它们已经在特征本身上定义了。

于 2013-05-19T00:14:48.887 回答