scala 2.8 中从 String 到 Int 的隐式转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1726163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implicit conversion from String to Int in scala 2.8
提问by Don Mackenzie
Is there something I've got wrong with the following fragment:-
以下片段是否有问题:-
object Imp {
implicit def string2Int(s: String): Int = s.toInt
def f(i: Int) = i
def main(args: Array[String]) {
val n: Int = f("666")
}
}
I get the following from the 2.8 compiler:-
我从 2.8 编译器得到以下内容:-
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
...\scala-2.8-tests\src\Imp.scala
Error:Error:line (4)error: type mismatch;
found : String
required: ?{val toInt: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method string2Int in object Imp of type (s: String)Int
and method augmentString in object Predef of type (x:String)scala.collection.immutable.StringOps
are possible conversion functions from String to ?{val toInt: ?}
implicit def string2Int(s: String): Int = s.toInt
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
...\scala-2.8-tests\src\Imp.scala
Error:Error:line (4)error: type mismatch;
found : String
required: ?{val toInt: ?}
请注意,隐式转换不适用,因为它们是不明确的:
类型为 (s: String)Int 的对象 Imp 中的
方法 string2Int 和类型为 (x:String) 的对象 Predef 中的方法增加字符串)scala.collection.immutable.StringOps
是从 String 到 ?{val toInt: ?}
隐式 def string2Int(s: String): Int = s.toInt 的 可能转换函数
回答by Daniel C. Sobral
What is happening is that Java does not define a toIntmethod on String. In Scala, what defines that method is the class StringOps(Scala 2.8) or RichString(Scala 2.7).
发生的事情是 Java 没有toInt在String. 在 Scala 中,定义该方法的是类StringOps(Scala 2.8) 或RichString(Scala 2.7)。
On the other hand, there is a method toIntavailable on Intas well (through another implicit, perhaps?), so the compiler doesn't know if it is to convert the string to StringOps, through the defined implicit, or to Int, through your own implicit.
另一方面,也有一个方法toInt可用Int(通过另一个隐式,也许?),所以编译器不知道它是StringOps通过定义的隐式将字符串转换为,还是Int通过您自己的隐式转换为.
To solve it, call the implicit explicitly.
要解决它,请显式调用隐式。
object Imp {
implicit def string2Int(s: String): Int = augmentString(s).toInt
def f(i: Int) = i
def main(args: Array[String]) {
val n: Int = f("666")
}
}
回答by oxbow_lakes
There is already an implicit conversion in scope, from scala.Predef. You don't need to declare your own implicit conversion to add a toIntmethod to a String. You have 3 options (I'd go for the last one!):
目前已经在范围上的隐式转换,从scala.Predef。您无需声明自己的隐式转换即可将toInt方法添加到String. 您有 3 个选择(我会选择最后一个!):
- Change your method name to something like
asInt - Unimport the conversion in
Predef - Don't bother defining your own and use instead the
toIntthat comes bundled with the scala library
- 将您的方法名称更改为类似
asInt - 取消导入转换
Predef - 不要费心定义自己的,而是使用
toInt与 scala 库捆绑在一起的
Note that scala will only make use of an in-scope implicit conversion if it is unique.
请注意,如果它是唯一的,scala 只会使用范围内的隐式转换。
回答by Don Mackenzie
I think I have a workaround.
我想我有一个解决方法。
If I create a RichString from the String argument, the implicit conversion occurs from RichString to Int using the implicit method I provide (this works for 2.7.x and 2.8). If I remove my implicit I get a type error.
如果我从 String 参数创建 RichString,则使用我提供的隐式方法(这适用于 2.7.x 和 2.8)发生从 RichString 到 Int 的隐式转换。如果我删除我的隐式,我会收到一个类型错误。
object Imp {
implicit def string2Int(rs: RichString): Int = rs.toInt
def f(i: Int) = i
def main(args: Array[String]) {
val n: Int = f(new RichString("666"))
println(n)
}
}
I'm still confused as to why both implicits came into scope and clashed when I provided an implicit and as to why the Predef one didn't come into scope when I didn't provide one for String to Int. I suppose the question about an implicit conversion from String to Int remains open.
我仍然很困惑,为什么当我提供一个隐式时,两个隐式都进入了范围并发生了冲突,以及为什么当我没有为 String 到 Int 提供一个 Predef 时,为什么 Predef 没有进入范围。我想关于从 String 到 Int 的隐式转换的问题仍然存在。

