【发布时间】:2010-12-16 02:50:39
【问题描述】:
我对以下片段有什么问题吗:-
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")
}
}
我从 2.8 编译器得到以下信息:-
信息:编译完成,出现 1 个错误和 0 个警告
信息:1 个错误
信息:0 个警告
...\scala-2.8-tests\src\Imp.scala
错误:错误:第 (4) 行错误:类型不匹配;
找到:字符串
必需:?{val toInt: ?}
请注意,隐式转换不适用,因为它们不明确:
类型为 (s: String)Int
的对象 Imp 中的两个方法 string2Int
和 (x:String) 类型的对象 Predef 中的方法 augmentString scala.collection.immutable.StringOps
是从 String 到 ?{val toInt: ?}
的可能转换函数
隐式 def string2Int(s: String): Int = s.toInt
【问题讨论】: