首页 > 解决方案 > Scala 隐式函数

问题描述

我正在尝试在 Scala 中遵循代码。

implicit def str2Int(x:String):Int = x.toInt

但我收到错误

Solution.scala:37: error: type mismatch;
 found   : x.type (with underlying type String)
 required: ?{def toInt: ?}

Note that implicit conversions are not applicable because they are ambiguous:
 both method augmentString in object Predef of type (x: String): scala.collection.StringOps
 and method str2Int in object Result of type (x: String): Int
 are possible conversion functions from x.type to ?{def toInt: ?}
    implicit def str2Int(x:String):Int = x.toInt
                                         ^
Solution.scala:37: error: value toInt is not a member of String
    implicit def str2Int(x:String):Int = x.toInt

我不知道这里有什么问题。请建议。

标签: scala

解决方案


推荐阅读