Question

How do I get this to compile?

Code

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)
  }
}

Error type mismatch; found : java.lang.String("cache 2.00") required: String

Était-ce utile?

La solution

Here's a version of the code that compiles:

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)
  }
}

You duplicated the types again in the wrap function and you did not need to as they were already defined on the trait itself.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top