I get an unexpected compilation error (in scala 2.11.8) when attempting to make an implicit class with a method named clone
.
The following simplified usage:
class Foo(val bar: String)
object Foo {
implicit class Enrich(foo: Foo) {
def clone(x: Int, y: Int): Int = x + y
}
}
object Main extends App {
val foo = new Foo("hello")
println(foo.clone(1, 2)) // <- does not compile
}
generated the following error:
method clone in class Object cannot be accessed in Foo Access to protected method clone not permitted because prefix type Foo does not conform to object Main where the access take place
However, I can manually apply the implicit class and it successfully compiles:
println(Foo.Enrich(foo).clone(1, 2)) // <- OK
If I rename the method to something else (clone2
, for example) the code compiles as expected.
I assume this is somehow related to the magic around java.lang.Cloneable
, but that method does not expect to parameters.
So what is going on here?