Я пишу следующее неявное преобразование в scala:
implicit def strToInt2(str: String):Int = {
str.toInt
}
Но возникает эта ошибка компиляции:
<console>:9: error: type mismatch;
found : str.type (with underlying type String)
required: ?{val toInt: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method augmentString in object Predef of type (x: String)scala.collection.
immutable.StringOps
and method toi in object $iw of type (str: String)Int
are possible conversion functions from str.type to ?{val toInt: ?}
str.toInt
^
Если я удаляю возвращаемый тип, просто объявите его следующим образом:
implicit def strToInt2(str: String) = {
str.toInt
}
Успешно компилируется.Может кто-нибудь сказать мне, в чем разница между этими двумя?