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?
The spec says you can access protected members from:
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.