Why does this code fail to compile, but compiles successfully when I uncomment the indicated line? (I'm using Scala 2.8 nightly). It seems that explicitly calling string2Wrapper
allows it to be used implicitly from that point on.
class A {
import Implicits.string2Wrapper
def foo() {
//string2Wrapper("A") ==> "B" // <-- uncomment
}
def bar() {
"A" ==> "B"
"B" ==> "C"
"C" ==> "D"
}
object Implicits {
implicit def string2Wrapper(s: String) = new Wrapper(s)
class Wrapper(s: String) {
def ==>(s2: String) {}
}
}
}
Edit: thanks for the answers so far, which include a pointer to Martin Odersky's comment,
"An implicit conversion without explicit result type is visible only in the text following its own definition. That way, we avoid the cyclic reference errors."
I'd still be interested in finding out 1) what is the danger of "cyclic reference errors"?, and 2) Why does an explicit call make any difference?