我想添加約束到一個特質的類型字段作爲額外的特徵混入,但似乎無法找到一種方式來表達任何幫助讚賞:是否有可能「混入:附加類型約束
trait Events { def be() = println("B") }
trait Create { self : Events => def create() = println("C") }
trait Update { self : Events => def update() = println("U") }
trait Delete { self : Events => def delete() = println("D") }
trait EventHandler {
type MyEvents <: Events
val myEvents : MyEvents
}
trait Creation {
self: EventHandler =>
override type MyEvents <: Create
}
def h(events: EventHandler with Creation) : Unit = {
events.myEvents.be() // <== wont' compile
events.myEvents.create()
}
編譯器說:
<console>:12: error: value be is not a member of events.MyEvents
見下面我修改後的答案,這是我的一個學習項目,以及一個例子 - 我會盡量遠離這麼多自種 – LaloInDublin