[英]Scala implicit conversions order
I have two objects with similar method ~(...). 我有两个类似方法的对象〜(...)。 I also defined implicit conversions which should convert pair (String, A) to either DemoObject or WopWopWop
我还定义了隐式转换,它应该将pair(String,A)转换为DemoObject或WopWopWop
class DemoObject[A](t: (String, A)) {
def ~(t: (String, A)) = "demo"
}
class WopWopWop[A](t: (String, A)) {
def ~(t: AnyVal) = "wop wop wop"
}
object ImplicitDemoConversions {
implicit def pair2DemoObject[A](t: (String, A)) = new DemoObject(t)
}
object ImplicitWopWopWopConversions {
implicit def pair2WopWopWop[A](t: (String, A)) = new WopWopWop(t)
}
However, having something like 但是,有类似的东西
import ImplicitDemoConversions._
object Hello {
def main(args: Array[String]): Unit = {
import ImplicitWopWopWopConversions._
val pair = ("LolTest" -> "A") ~ ("whoa" -> "wop wop wop")
println(pair)
}
}
will print answer demo instead of expected wop wop wop . 将打印答案演示而不是预期的wop wop wop 。 Looks like scala compiler ignores second import of ImplicitWopWopWopConversions._
看起来像scala编译器忽略了ImplicitWopWopWopConversions._的第二次导入。
Question is why do I think that pair (String, String) should be converted to WopWopWop and how do I get WopWopWop object instead of DemoObject? 问题是为什么我认为对(String,String)应该转换为WopWopWop ,我如何获得WopWopWop对象而不是DemoObject?
You can find real example here https://github.com/json4s/json4s/issues/121 你可以在这里找到真实的例子https://github.com/json4s/json4s/issues/121
You can hide the import with an alias. 您可以使用别名隐藏导入。
import ImplicitDemoConversions.{pair2DemoObject => i}
object Hello {
def main(args: Array[String]): Unit = {
import ImplicitWopWopWopConversions.{pair2WopWopWop => i}
val pair = ("LolTest" -> "A") ~ ("whoa" -> "wop wop wop")
println(pair)
}
}
Edit 编辑
The reason that your tuple is being converted to a DemoObject
instead of a WopWopWop
is because DemoObject
's ~
method argument type is more specific than WopWopWop
's. 你的元组被转换为
DemoObject
而不是WopWopWop
的原因是因为DemoObject
的~
方法参数类型比WopWopWop
更具体。
It's just like this example: 就像这个例子一样:
object A {
def m(x: AnyVal) { println(x) }
def m(x: (Int, String)) { println(x) }
// This will call the first method, because the second method can't be
// applied to an Int
a(1)
// Either method could be applied to a (Int, String), but the second
// method will be chosen, because it's argument type is more specific.
a((1 -> "hello"))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.