Scala: Accessing protected field of companion object's trait
scala
Solution
The spec says you can access protected members from:
the companion module of any of those classes [that have the defining template as a base].
That is, not from the companion class of an object that has the defining template as a base. Tricky.
This is not obvious because of the "module" nomenclature, where module simply means object. There is occasional talk about changing that. Although classes and modules can be companions, the relation is not symmetric; consider implicit search.
trait A {
protected var foo = "Foo"
protected def bar = "Bar"
}
object B extends A {
//override protected var foo = super.foo // no
override protected def bar = super.bar
}
class B {
//println(B.foo) // no
println(B.bar) // ok
}
class C extends A
object C {
println(new C().foo) // ok
}
Problem
I have a Trait, a Companion Object and a Class in Scala: ``` trait A { protected var foo = "Foo" } object B extends A { } class B { println(B.foo) } ``` Why can't I access foo? I thought that foo would become a field of the object "B". Is there a way to do this?