简体   繁体   English

类型推断和字符串文字,该怎么做?

[英]type inference and string literals, how to do it?

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 找到:java.lang.String(“ cache 2.00”)必需:字符串

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. 您可以在wrap函数中再次复制类型,而不必,因为已经在特征本身上定义了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM