以下片段有什么问题吗:-
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)错误:类型不匹配;
found : String
required: ?{val toInt: ?}
请注意,隐式转换不适用,因为它们不明确:
类型为 (s:String)Int 的对象 Imp 中的
方法 string2Int 和类型为 (x:String) 的对象 Predef 中的方法 augmentString )scala.collection.immutable.StringOps
是从 String 到 ?{val toInt: ?} 的可能转换函数
implicit def string2Int(s: String): Int = s.toInt