我会定义一个新的Function1
并混合一个特征,以实现一种轻量级可堆叠的特征模式。
我尝试过,但它没有编译
scala> trait Filtering extends (String => Unit) { abstract override def apply(s: String) { if (s.startsWith("foo")) super.apply(s) }}
defined trait Filtering
scala> val f = new Function1[String, Unit] {def apply(s: String) = println(s)} with Filtering
<console>:1: error: ';' expected but 'with' found.
val f = new Function1[String, Unit] {def apply(s: String) = println(s)} with Filtering
我想避免创建一个类:
class Default extends (String => Unit) {
def apply(s : String) = println(s)
}
val f = new Default with Filtering