禁止混合特定的特征

时间:2012-12-23 11:07:44

标签: scala type-systems

假设:

trait Foo
trait Bar { this: Foo => }
trait NoBar { this: Foo => }

有没有办法可以欺骗类型系统禁用:

new Foo with Bar with NoBar {}

1 个答案:

答案 0 :(得分:12)

类型擦除再次挽救了一天:

trait Foo
trait Dummy[A]
trait Bar extends Dummy[Bar]{ this: Foo => }
trait NoBar extends Dummy[NoBar]{ this: Foo => }
new Foo with Bar with NoBar {}

这会导致以下错误:

illegal inheritance; anonymous class $anon inherits different
type instances of trait Dummy: Dummy[Bar] and Dummy[NoBar]