To explain my question:
Class : Toy
Trait1: Speak like Male
Trait2: Speak like Female
Can I change the behavior (traits) of Toy during runtime so
sealed trait Speaker
case object Male extends Speaker
case object Female extends Speaker
class Toy(name: String, speaks: Speaker = Male) {
def speak = speaks match {
case Male => "ugh"
case Female => "What time do you call this?"
}
}
Then
barbie = ken.copy(speaks = Female)
You cannot change the trait
s which an object extends at runtime, because a trait
is mixed in to create a class (in a .class file). A given object has exactly one class and this can never be changed.