scala: defining a tratit and referencing the corresponding companion object
Posted
by
opensas
on Stack Overflow
See other posts from Stack Overflow
or by opensas
Published on 2012-06-04T04:04:11Z
Indexed on
2012/06/04
4:40 UTC
Read the original article
Hit count: 187
I'm trying to define a trait that uses the corresponding companion object, that is, the componion object of the class using the trait.
for example, I have:
:paste
class Parent {
def callMyCompanion = print(Parent.salute)
}
object Parent {
def salute = "Hello from Parent companion object"
}
class Child extends Parent {
}
object Child {
def salute = "Hello from Child companion object"
}
And then I create a parent object:
scala> val p = new Parent()
p: Parent = Parent@1ecf669
scala> p.callMyCompanion
Hello from Parent companion object
But with a child:
scala> val c = new Child()
c: Child = Child@4fd986
scala> c.callMyCompanion
Hello from Parent companion object
I'd like to get: Hello from Child companion object
How can I achieve it???
© Stack Overflow or respective owner