This does not work as expected (since I am trying to call a package private run
from outside Services
):
object Services {
class HelloPrinter {
private[Services] def run = "Hello"
}
}
val obj = new Services.HelloPrinter
But, surprisingly this works:
val obj: {def run: String} = new Services.HelloPrinter
obj.run
I would say, its a bug in the compiler since as HelloPrinter does not match the structural type because of package visibility rules, it should not compile at all!
Here is a case where the program compiles but it throws a runtime exception (java.lang.NoSuchMethodException
):
class HelloPrinter {
private[HelloPrinter] def run = "Hello"
}
val obj: {def run: String} = new HelloPrinter
obj.run
Is this a language feature or rule I am missing or legitimately a bug in Scala?
scala -feature
REPL? It clearly shows that there is a reflective call invoked. I guess scoping and immutability are all out of the window when it comes to reflection. – SelfdefenseNoSuchMethodException
doesn't compile for me (in the 2.10.4 and 2.11.6 REPL). – Forbidden