Best practice to implement Scala trait which suppo

2019-02-12 10:26发布

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)

3条回答
叼着烟拽天下
2楼-- · 2019-02-12 10:56

You can explicitly specify the instance type as the return type for setProp.

trait SomeProperty {
    var prop : String = "default"
    def setProp(s: String):this.type = {
        prop = s
        this
    }
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-12 11:15

Not sure if this is what you're looking for

scala> trait Property[T] {
     |   me: T =>
     |   var prop:String=""
     |   def setProp(s:String) = {
     |     prop=s
     |     me
     |   }
     | }
defined trait Property

scala> class A extends Property[A]
defined class A

scala> class B extends Property[B]
defined class B

scala> val a= new A
a: A = A@694a18

scala> val b = new B
b: B = B@1108691

scala> a.setProp("Hi")
res13: Property[A] with A = A@694a18

scala> a.setProp("Hi").setProp("Bye")
res14: Property[A] with A = A@694a18

scala> b.setProp("D")
res15: Property[B] with B = B@1108691
查看更多
smile是对你的礼貌
4楼-- · 2019-02-12 11:16

Easiest thing to do is use a generic.

object Value {

  trait SomeProperty[X] {
    var str: String = null;
    def setStr(s: String): X = {
      str = s;
      return this.asInstanceOf[X]
    }
  }

  abstract sealed class Value
  case class IntegerValue(i: Int)
  case class StringValue(s: String) extends SomeProperty[StringValue] {
    def foo(): Unit = {
      println("Foo.")
    }
  }
  case class UnknownValue(o: Any) extends SomeProperty[UnknownValue] {
    def bar(): Unit = {
      println("Bar.")
    }
  }

  def main(args: Array[String]): Unit = {

    new UnknownValue(18).setStr("blah blah blah").bar
    new StringValue("A").setStr("halb halb halb").foo
  }
}
查看更多
登录 后发表回答