隐式转换,是否需要导入?
我写
object MyString {
implicit def stringToMyString(s: String) = new MyString(s)
}
class MyString(str: String) {
def camelize = str.split("_").map(_.capitalize).mkString
override def toString = str
}
object Parse {
def main(args: Array[String]) {
val x = "active_record".camelize
// ...
}
}
在我的程序中。这会导致编译错误。我插入后
import MyString.stringToMyString
然后就可以了。
从 Odersky 的 Scala 编程 中,我发现不需要导入源或预期目标类型的伴生对象中的隐式转换。
I write
object MyString {
implicit def stringToMyString(s: String) = new MyString(s)
}
class MyString(str: String) {
def camelize = str.split("_").map(_.capitalize).mkString
override def toString = str
}
object Parse {
def main(args: Array[String]) {
val x = "active_record".camelize
// ...
}
}
in my program. This causes a compiling error. After I inserted
import MyString.stringToMyString
Then it works.
From Odersky's Programming in Scala I got that implicit conversion in the companion object of the source or expected target types don't need to be imported.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实如此。现在,方法
camelize
是在类MyString
上定义的,并且实际上,在其对象同伴内部存在到MyString
的隐式转换。但是,代码中没有任何内容告诉编译器MyString
是预期目标类型。相反,如果您这样写:
那么它就会起作用,因为编译器会知道您期望
"active_record"
是一个MyString
,使得它查找对象MyString
内部的隐式转换。这可能看起来有点限制,但它实际上在很多地方都有效。举例来说,您有:
然后您有这样的代码:
现在,
x
确实有一个+
方法,其预期类型是分数
。因此,编译器会在此处查找对象Fraction
内(以及对象Int 内)从
,如果有的话,因为那是源类型)。Int
到Fraction
的隐式转换True enough. Now, the method
camelize
is defined on the classMyString
, and, indeed, there is an implicit conversion toMyString
inside its object companion. However, there is nothing in the code telling the compiler thatMyString
is the expected target type.If, instead, you wrote this:
then it would work, because the compiler would know you expect
"active_record"
to be aMyString
, making it look up the implicit conversion inside objectMyString
.This might look a bit restrictive, but it actually works in a number of places. Say, for instance, you had:
And then you had a code like this:
Now,
x
does have a+
method, whose expected type isFraction
. So the compiler would look, here, for an implicit conversion fromInt
toFraction
inside the objectFraction
(and inside the objectInt
, if there was one, since that's the source type).在这种情况下,您需要导入,因为编译器不知道您从哪里提取
camelize
方法。如果类型明确,它将在不导入的情况下进行编译:请参阅 Pimp 我的库模式 ,基于 Martin 的文章:
In this situation you need the import because the compiler doesn't know where you pulled out the
camelize
method from. If the type is clear, it will compile without import:See Pimp my library pattern, based on Martin's article:
我尝试了《Scala 编程》一书中的 Rational 类示例,在其伴生对象中放置了一个隐式方法:
但代码
不起作用。为了发生转换,应用单一标识符规则,即,您需要将显式方法导入到作用域中,即使它是在伴生对象中定义的。
I tried the Rational class example in Programming in Scala book, put an implicit method in its companion object:
but the code
does not work. For the conversion to happen, the single identifier rule applies, i.e., you need to import the explicit method into scope even though it is defined in the companion object.