Skip to content

spec is silent whether access modifiers propagate inward; implementation is inconsistent on private vs. protected #9458

Open
@scabug

Description

@scabug

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:
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions