escribo la siguiente conversión implícita en Scala:¿Por qué este tipo de conversión implícita es ilegal?
implicit def strToInt2(str: String):Int = {
str.toInt
}
pero se eleva este error de compilación:
<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
^
Si quito el tipo de retorno, simplemente declaran así:
implicit def strToInt2(str: String) = {
str.toInt
}
Compila con éxito. ¿Alguien puede decirme cuál es la diferencia entre los dos?
No sé la respuesta exacta, pero supongo que hay una cadena de conversión implícita -> int en Predef. Entonces, agregar nuevas conversiones de ese tipo hace que las cosas sean ambiguas. – dmitry