[英]Trait self type bound: A with B but not A with C
說我有:
abstract class D[T] {}
trait A[T] { self => D[T] without B }
trait B[T] { self => D[T] without A }
底線,如果B
已經延伸A
,則不能混入D.
class Test extends D[String] with B[String] // ok
class Test2 extends D[String] with A[String] // ok
class Test3 extends D[String] with A[String] with B[whatever] // bad
class Test4 extends D[String] with B[String] with A[whatever] // bad
如何正確執行此類型綁定?
有一種方法可以做到這一點,雖然我不知道它是否是唯一/首選方式。
sealed trait Z[T <: Z[T]]
trait A extends Z[A] { this: D => }
trait B extends Z[B] { this: D => }
scala> new D with A
res6: D with A = $anon$1@7e7584ec
scala> new D with B
res7: D with B = $anon$1@7537e98f
scala> new D with A with B
<console>:15: error: illegal inheritance;
anonymous class $anon inherits different type instances of trait Z:
Z[B] and Z[A]
new D with A with B
^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.