You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
packagep {
traitA {
private[p] deff():String="A"
}
traitBextendsA {
// Here we're implementing a method with the same signature// as an inherited one, and we can see the inherited one, and// we are not required to override.private[B] deff():String="B/"+super.f()
defhmm(x: B):String= {
// Both implementations are visible externally, depending on static// type of receiver.vals1= (x: A).f()
vals2= (x: B).f()
""+ ((s1, s2))
}
}
traitCextendsB {
// Written like this, fails as shown://// private[p] def f(): String = "C"// a.scala:16: error: overriding method f in trait A of type ()String;// method f needs `override' modifier// private[p] def f(): String = "C"// ^// one error found//// But we can't call the method we're required to override!// Written like this, fails as shown://// override private[p] def f(): String = "C" + super.f()// a.scala:27: error: method f in trait B cannot be accessed in p.B// override private[p] def f(): String = "C" + super.f()// ^// one error found
}
objectTest {
defmain(args: Array[String]):Unit= {
valb=newB { }
println(b hmm b)
// prints: (A,B/A)
}
}
}
packagep1.p2 {
abstractclassA {
private[p2] deff():String="A"overridedeftoString=s"A:f=$f"
}
classC1extendsA {
// Compiles, extending A directlyoverridedeftoString=super.toString +""+s"C1:f=$f"
}
classC2extends p1.p3.B1 {
// SHOULD compile - the f() defined in the superclass B1 is private,// so this f should refer to the f() inherited from A, which is visible// to us since we are in the same package. The f() defined in B1 should// not affect us in any way. That's what "private" means.overridedeftoString=super.toString +""+s"C2:f=$f"// a.scala:10: error: method f in class B1 cannot be accessed in p1.p2.C2// override def toString = super.toString + " " + s"C2:f=$f"// ^// one error found
}
classC3extends p1.p3.B2 {
// Compiles, extending B2 which extends A.overridedeftoString=super.toString +""+s"C3:f=$f"
}
}
packagep1.p3 {
abstractclassB1extends p1.p2.A {
// SHOULD compile - the f() defined in the superclass A is private with a// scope which excludes us. That means it should not affect us in any way.// That's what "private" means.privatedeff():String="B1"// a.scala:24: error: overriding method f in class A of type ()String;// method f has weaker access privileges; it should not be private// private def f(): String = "B1"// ^// one error foundoverridedeftoString=super.toString +""+s"B1:f=$f"
}
abstractclassB2extends p1.p2.A {
// Letting f pass throughoverridedeftoString=super.toString +""+"B2"
}
}
objectTest {
defmain(args: Array[String]):Unit= {
println(new p1.p2.C1)
println(new p1.p2.C2)
println(new p1.p2.C3)
}
}
@retronym said:
Martin suggests we should revisit the wording in the spec regarding qualified private. Currently, it says: "such members are also inherited only from templates inside C ."
Thanks to Paul for the test cases.
This is a followup to #8143
The text was updated successfully, but these errors were encountered: