I’m having difficulties with the private annotation of inner class methods and constructors. While this works as expected:
trait A {
protected def lala = ()
}
trait B extends A {
lala
}
The following doesn’t:
trait A {
class Lala protected()
}
trait B extends A {
new Lala
}
Neither does this:
trait A {
class Lala private[A]()
}
trait B extends A {
new Lala
}
The only way around is something like this:
object Screwed {
trait A {
class Lala private[Screwed]()
}
trait B extends A {
new Lala
}
}
Does Scala really fail here to provide a clear mechanism, or am I missing something? My guess would have been that it should be private[this.type] but scalac doesn’t want to swallow that at all…
Well,
doesn’t compile either. The error message seems pretty reasonable:
Protected access means you can only access that constructor from that class or sub-classes. You’re trying to call it from the enclosing trait. One thing you can do is this:
I wouldn’t expect your third example to compile, for similar reasons.
Also note the difference between a protected constructor and a
protected class. So for example: