从伴随对象导入隐式值但未使用
Import implicit values from a companion object but not used
我写了一些代码来获取伴随对象中的一些隐式值,如下所示:
package example.implicits
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
object Test {
trait GetValue[T] {
def value: T
}
object Implicits {
implicit val intValue = new GetValue[Int] {
def value = 10
}
implicit val stringValue = new GetValue[String] {
def value = "ten"
}
}
}
这段代码无法编译,编译器抱怨找不到所需的隐式值。注意我的环境是
scala 2.11.8 on Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66
但是,如果我明确使用这些值,则不会出错:
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int](intValue)
val stringV = getV[String](stringValue)
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
此外,如果我声明新的隐式值如下:
class Test {
import Test.GetValue
import Test.Implicits._
implicit val intValue1 = intValue
implicit val stringValue1 = stringValue
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
由于隐式值不明确,将引发错误。
当我交换 class 测试和对象测试的位置时,一切正常:
object Test {
trait GetValue[T] {
def value: T
}
object Implicits {
implicit val intValue = new GetValue[Int] {
def value = 10
}
implicit val stringValue = new GetValue[String] {
def value = "ten"
}
}
}
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
那么为什么在第一种情况下我已经导入隐式值后 scala 不能找到它们?
为什么我交换他们的位置时它可以这样做?
那是因为编译器在解析 getV[Int]
.
中的隐式时还没有推断出 Test.intValue
的类型
只需明确给出 Test.intValue
和 Test.stringValue
它们的类型,您的问题就会得到解决。
我在某处读到(抱歉,不记得确切的位置),应该始终为隐式定义指定一个显式类型,特别是为了避免这种行为。
我写了一些代码来获取伴随对象中的一些隐式值,如下所示:
package example.implicits
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
object Test {
trait GetValue[T] {
def value: T
}
object Implicits {
implicit val intValue = new GetValue[Int] {
def value = 10
}
implicit val stringValue = new GetValue[String] {
def value = "ten"
}
}
}
这段代码无法编译,编译器抱怨找不到所需的隐式值。注意我的环境是
scala 2.11.8 on Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66
但是,如果我明确使用这些值,则不会出错:
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int](intValue)
val stringV = getV[String](stringValue)
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
此外,如果我声明新的隐式值如下:
class Test {
import Test.GetValue
import Test.Implicits._
implicit val intValue1 = intValue
implicit val stringValue1 = stringValue
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
由于隐式值不明确,将引发错误。
当我交换 class 测试和对象测试的位置时,一切正常:
object Test {
trait GetValue[T] {
def value: T
}
object Implicits {
implicit val intValue = new GetValue[Int] {
def value = 10
}
implicit val stringValue = new GetValue[String] {
def value = "ten"
}
}
}
class Test {
import Test.GetValue
import Test.Implicits._
val intV = getV[Int]
val stringV = getV[String]
private def getV[T](implicit getV: GetValue[T]): T = getV.value
}
那么为什么在第一种情况下我已经导入隐式值后 scala 不能找到它们?
为什么我交换他们的位置时它可以这样做?
那是因为编译器在解析 getV[Int]
.
Test.intValue
的类型
只需明确给出 Test.intValue
和 Test.stringValue
它们的类型,您的问题就会得到解决。
我在某处读到(抱歉,不记得确切的位置),应该始终为隐式定义指定一个显式类型,特别是为了避免这种行为。