有没有办法定义一个常见类型的替代品集合:
trait Mutability
trait Mutable extends Mutability
trait Immutable extends Mutability
Run Code Online (Sandbox Code Playgroud)
并让编译器排除类似的东西:
object Hat extends Mutable with Immutable
Run Code Online (Sandbox Code Playgroud)
我相信我可以通过拥有一个共同的,冲突的成员来强制一些编译器错误,但错误消息有点倾斜:
trait Mutability
trait Mutable extends Mutability { protected val conflict = true }
trait Immutable extends Mutability { protected val conflict = true }
object Hat extends Mutable with Immutable
<console>:10: error: object Hat inherits conflicting members:
value conflict in class Immutable$class of type Boolean and
value conflict in class Mutable$class of type Boolean
(Note: this can be resolved …Run Code Online (Sandbox Code Playgroud)