I want to make a trait which add some property to a class and make it possible to chain methods. Tested in Scala 2.8.1.
trait SomeProperty {
var prop : String = "default"
def setProp(s: String) = {
prop = s
this
}
}
sealed abstract class Value
case class IntegerValue(v: Int) extends Value
case class FloatValue(v: Float) extends Value with SomeProperty {
def foo() = { println("I'm foo.") }
}
case object UnknownValue extends Value with SomeProperty {
def bar() = { println("I'm bar.") }
}
scala> val x = UnknownValue
scala> x.setProp("test").bar()
<console>:10: error: value bar is not a member of SomeProperty
x.setProp("test").bar()
What is the most common practice in this kind of situation? (Type-safe way is preferred)