Description
The compiler sometimes allows access to members of inaccessible types (note protected
):
scala> object A { protected class C { def x = 1 }; val c = new C }
defined object A
scala> A.c.x
res3: Int = 1
but sometimes prohibits constructing such a situation entirely (note private
):
scala> object C { private class D; val d = new D }
<console>:10: error: private class D escapes its defining scope as part of type C.D
object C { private class D; val d = new D }
I cannot find any language in the specification that actually addresses this directly. In the absence of such language, I don't see any justification for the latter error.
Previous discussions:
- 2012:
- https://groups.google.com/d/msg/scala-user/Z98mibojfvQ/SNg1MZRKpWIJ
- https://groups.google.com/d/msg/scala-user/UyWS5X9CH-g/24UzB8nV0u4J
- 2015:
Both 2012 threads ended without any real resolution.
In the 2015 thread, @retronym notes that Java forbids even the protected
version (and he quotes explicit language in the JLS specifying that). It's unclear where this difference between Scala and Java is accidental or intentional. The fact that scalac forbids the private
version outright suggests to me that the lack of corresponding language in the SLS is an oversight.