是否有可能从不同的特征中链接方法?

时间:2011-08-05 11:31:29

标签: scala

我有以下代码:

class Parameterizable{
  var map: Map[String, String] = new scala.collection.immutable.HashMap() 
  def put(entry: Tuple2[String, String]) = {
    map = map + entry; this
  }
}

class Query() extends Parameterizable{
  override def toString = {
    map.isEmpty match{
      case true => ""
      case false => "?" + map.map{case (key, value) => key + "=" + value}.mkString("&")
    }
  }
}

trait PageParameter extends Parameterizable{
  def page(page: Int) = put(("page" -> page.toString))
  def pageSize(pageSize: Int) = put(("pagesize" -> pageSize.toString))
}

trait DateParameter extends Parameterizable{
  def fromDate(date: java.util.Date) = put(("fromdate" -> (date.getTime()/1000L).toString()))
  def toDate(date: java.util.Date) = put(("todate" -> (date.getTime()/1000L).toString()))
}
//and other similar traits

我想做点什么:

class ExtendedQuery extends Query with PageParameter with DateParameter
val query = new ExtendedQuery
query.page(4).pageSize(5).fromDate(new java.util.Date)

或:

query.and().page(4).and().pageSize(5).and().fromDate(new java.util.Date)

Scala可以吗?

1 个答案:

答案 0 :(得分:11)

您可以将方法声明为返回this.type,然后从中返回this

trait PageParameter extends Parameterizable{
  def page(page: Int) : this.type = { put(("page" -> page.toString)); this }
  def pageSize(pageSize: Int): this.type = { put(("pagesize" -> pageSize.toString)); this }
}

在使用现场,您可以根据需要链接呼叫。见这个例子:

scala> trait Wibble {
 | def foo : this.type = { println("foo"); this }
 | }
defined trait Wibble

scala> trait Wobble extends Wibble {
 | def bar: this.type = { println("bar"); this }
 | }
defined trait Wobble

scala> trait Wubble extends Wibble {
 | def baz: this.type = { println("baz"); this }
 | }
defined trait Wubble

现在我可以测试了它

scala> new Wibble with Wobble with Wubble
res0: java.lang.Object with Wibble with Wobble with Wubble = $anon$1@937e20

scala> res0.bar.baz.foo
bar
baz
foo
res1: res0.type = $anon$1@937e20