鉴于以下特征和类别。为什么要编译?这实际上可以用于某些事情吗?
trait Container {
type A
}
trait AnotherContainer[B]{
def x(b : B) : B
}
trait Mixed extends Container with AnotherContainer[Container#A]
class Impl extends Mixed{
def x(a : Container#A) = a
}
new Impl().x _
scala> new Impl().x _
res0: (Container#A) => Container#A = <function>
更新:
class Baz { type T; }
实际上是一个功能,但我找不到它的动机:#1753 .
最佳答案
在您的示例中,编译器添加了 >: Nothing <: Any
的默认类型边界。 .下面的第二个示例显示了抽象类型变得可用(如果没有用)的情况。
scala> trait T { type A >: Nothing <: Any }
defined trait T
scala> 1: T#A
<console>:6: error: type mismatch;
found : Int(1)
required: T#A
1: T#A
^
scala> trait T { type A >: Int <: Int }
defined trait T
scala> 1: T#A
res6: T#A = 1
scala> "": T#A
<console>:6: error: type mismatch;
found : java.lang.String("")
required: T#A
"": T#A
^
关于scala - 具有抽象类型成员的具体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115678/