我有一组有限的特征,比如说Trait1
到TraitN
,我想建立一个"复合材料" o
的装饰器混合在装饰的子集中(对于混合的每个特征)在运行时。 N = 2的例子:
class CommonImpl extends Trait1 ... with TraitN { ... }
trait Trait1Decoration extends Trait1 { ... } // override some methods
trait Trait2Decoration extends Trait2 { ... } // override some methods
val oDecorated = o match {
case t12: Trait1 with Trait2 => new CommonImpl with Trait1Decoration with Trait2Decoration
case t1: Trait1 => new CommonImpl with Trait1Decoration
case t2: Trait2 => new CommonImpl with Trait2Decoration
case _ => new CommonImpl // no decorations, could also return "o"
} // now, oDecorated has some subset of Traits decorated