I'm quite new to Scala, but I'm trying to implement the following situation. Suppose I have a trait:
trait SomeTrait {
def kakaw
}
And two Scala objects that extend it:
object SampleA extends SomeTrait {
def kakaw = "Woof"
}
object SampleB extends SomeTrait {
def kakaw = "Meow"
}
What I'd like to do is call one of these two object functions based on a parameterized function call. For example (and I know this is the furthest thing from correct):
class SomeOther {
def saySomething[T] = T.kakaw
}
So I can do something like:
val s = new SomeOther
s.saySomething[SampleA]
Is this at all possible in Scala?
It’s a bit confusing because you’ll need to have an instance of your type to act on. Just passing a type may make the compiler happy but you certainly want to make sure that you supply the very instance of some type you want to work with.
(Considering singleton objects there may be a work around using implicit evidence parameters for that, but I wouldn’t do that unless really needed.)
So, in your case why don’t you just say