为什么隐含的名称似乎会影响其在此示例中的范围解析?
Why does the name of an implicit seem to affect its scope resolution in this example?
我正在为 java.io.File
编写一个简单的 JSON 序列化程序,只是为了简化路径:
import java.io.File
import play.api.libs.json._
import Implicits.File._
object Implicits {
object File {
implicit val format: Format[File] = new Format[File] {
override def writes(o: File): JsValue = JsString(o.toString)
override def reads(js: JsValue): JsResult[File] = js.validate[String].map(f => new File(f))
}
}
}
final case class Bar(path: File)
object Bar {
implicit val format: Format[Bar] = Json.format
}
我发现上面的方法不起作用:
No instance of play.api.libs.json.Format is available for java.io.File in the implicit scope
但是,如果我将 Implicit.File.format
的名称更改为 Implicit.File.fmt
,它可以正常工作。
为什么名称在这种情况下会发生冲突,而隐式作用域解析器应该关心的 类型 、Format[File]
?
我正在使用 play-json 2.6.7.
Why does the name collide in this case when it should be the type, Format[File]
, that the implicit scope resolver should care about?
因为它也关心名字。
在 implicit val format: Format[Bar] = Json.format
行,format
表示 Bar.format
而不是 Implicits.File.format
,因此 Implicits.File.format
不符合此规则的隐含条件。而且它不在伴生对象中,因此也不在第二类中。
我正在为 java.io.File
编写一个简单的 JSON 序列化程序,只是为了简化路径:
import java.io.File
import play.api.libs.json._
import Implicits.File._
object Implicits {
object File {
implicit val format: Format[File] = new Format[File] {
override def writes(o: File): JsValue = JsString(o.toString)
override def reads(js: JsValue): JsResult[File] = js.validate[String].map(f => new File(f))
}
}
}
final case class Bar(path: File)
object Bar {
implicit val format: Format[Bar] = Json.format
}
我发现上面的方法不起作用:
No instance of play.api.libs.json.Format is available for java.io.File in the implicit scope
但是,如果我将 Implicit.File.format
的名称更改为 Implicit.File.fmt
,它可以正常工作。
为什么名称在这种情况下会发生冲突,而隐式作用域解析器应该关心的 类型 、Format[File]
?
我正在使用 play-json 2.6.7.
Why does the name collide in this case when it should be the type,
Format[File]
, that the implicit scope resolver should care about?
因为它也关心名字。
在 implicit val format: Format[Bar] = Json.format
行,format
表示 Bar.format
而不是 Implicits.File.format
,因此 Implicits.File.format
不符合此规则的隐含条件。而且它不在伴生对象中,因此也不在第二类中。