I know I can add a companion object to a class for static def/val but my problem is I want it to comfirm to a trait. I want access to this static val or def as an abstract type (using the word static here in terms of the behaviour expected from java static) Consider this code
trait A {
def aBehavior(): Unit
}
trait B {
def bBehavior(): Unit
def description: String
}
case class M extends A with B {
override def aBehavior() = print("A behavior of M")
override def bBehavior() = print("B behavior of M")
override def description = "I'm M"
}
I want to be able to call M.description as a static method without having an instance of M. My use case is I have a spark dataset of M objects and I want to see the description property of M without getting a record from the dataset because that will result in a spark action / job Is there a scala pattern I can use for this. Thanks
object M { def description = "I'm M" }
? I'm not sure what you mean by "confirm to a trait" in the context of static methods - you cannot "implement" or "override" static members on the JVM.object B
would do the trick then?B.description
and expected different results for each call, depending on ... huh? Well, I think, you gonna have to have some type information of what you want described!