I am looking at Akka related typesafe activator code and the following construct intrigued me:
Companion object:
object MarkerActor {
sealed trait MarkerMessage
case object Stop extends MarkerMessage
..
def objectMethod = print("hi from companion object")
}
Companion class: it imports the companion object methods:
class MarkerActor extends Actor with ActorLogging {
import MarkerActor._ // Comment this line to compare w or w/o import available
objectMethod // just to see if 'visible' within companion class
override def receive = {
case Stop => {
So.. that is a bit surprising. Why is there not a "special relationship" between the companion class/object allowing the class to "see" the object methods automatically?
Update I was a bit skeptical on this, and so went ahead and commented out the "import MarkerActor._" This resulted in "Symbol not found: Stop" errors in the Companion Class. So .. the import really is required.
Stop
is of typeStop.type
. The Scala spec says that private members of the companion object are visible in the class, but does that include types? If types inside the companion object are not visible in the class, then you cannot useStop
because its type would be private. – KingboltobjectMethod
private and still call it from the class. – Kingbolt