New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
qualified modifiers, private[C] and protected[C] handled incorrectly in a subclass #2568
Comments
Imported From: https://issues.scala-lang.org/browse/SI-2568?orig=1
|
jrosiek said: |
@adriaanm said: You're allowed to widen access in subclasses, just like in Java, so it's okay to do this: package pkg2 {
class A {
// Same with protected[pkg2]
private[pkg2] def f = 0
}
}
class B extends pkg2.A { // class A is visible here, so we can extend it and override its non-final members
// f is now public
override def f = 1
} |
jrosiek said: <<<<< I'm not sure what the last sentence mean (how can I inherit private[C] member from template not inside C? Such template cannot have private[C] member.), but certainly the remaining states that B cannot access A's private[pkg2] member. |
@odersky said: |
@paulp said:
That it actually works as it's supposed to if the qualifier is a class name means trial-and-error is the only way to discover the behavior. package s1 {
object O {
class A {
private[s1] def f1(): Int = 1
private[O] def f2(): Int = 2
private[A] def f3(): Int = 3
}
}
}
package s2 {
class B extends s1.O.A {
override def f1() = 11 // allowed
override def f2() = 12 // allowed
override def f3() = 13 // disallowed
// ./a.scala:15: error: method f3 overrides nothing
// override def f3() = 12
// ^
// one error found
}
} |
dotc also allows the f3 override in the last example. |
Clarified at #9321 in 2015 and at the linked dotty ticket. This is certainly a case where I was misled by outdated discussion, see my pre-pandemic comment. I just noticed that odersky's more sage comment was also Nov 23 exactly ten years earlier: it needs a SIP, or at least the spec update in the linked ticket. |
Scala compiler does allow to override private[C] or protected[C] member in a class that is defined outside of C. IMHO, according to the scala reference this should not be allowed. Please see attached source.
The text was updated successfully, but these errors were encountered: