Depending on your scalac version, there have been bugs where synthetic methods cause this error.
https://issues.scala-lang.org/browse/SI-6278
Illustration, imagine f is generated:
object Test {
def main(args: Array[String]) {
class NotUsed {val x = f}
val dummy = false
def f = true
}
}
Case classes, default arguments and implicit classes involve synthetics.
In the example code from that ticket (which has been fixed), you can break the ok method by moving the implicit to the end of the function:
object tiny {
def main(args: Array[String]) {
ok(); nope()
}
def ok() {
class Foo(val i: Int) {
def foo[A](body: =>A): A = body
}
implicit def toFoo(i: Int): Foo = new Foo(i)
val k = 1
k foo println("k?")
val j = 2
}
def nope() {
implicit class Foo(val i: Int) {
def foo[A](body: =>A): A = body
}
val k = 1
k foo println("k?")
//lazy
val j = 2
}
}
what can be the ways to fight it?
As implied by the comment in the code, making the definition lazy is a workaround.
Illustration 2, imagine the function is so long that you don't notice the naming problem:
object Test {
def main(args: Array[String]) {
class NotUsed {val xs = args}
val dummy = false
// oops, shadows the parameter
def args = Seq("a","b","c")
}
}