【发布时间】:2016-12-16 17:19:01
【问题描述】:
使用早期初始化语法时似乎有些微妙之处。
trait Base { def callMe = "callMe" }
trait Proxy { this: Base => def call = s"proxied: $callMe" }
val base1 = new Base { } // non-early init works
val baseFail = new { } with Base // error: trait Base is abstract; cannot be instantiated
val base2 = new { val n=1 } with Base // why does this fix the failure?
val proxy = new { } with Base with Proxy // why doesn't this fail?
为什么baseFail 行失败,而其他vals 行却没有?
错误信息也令人困惑——我不是要实例化Base,只是把它混进去。
【问题讨论】:
标签: scala traits anonymous-class