具有抽象类型成员的具体类

时间:2010-01-22 07:18:34

标签: scala types

鉴于以下特征和课程。为什么编译?这真的可以用于什么吗?

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

2 个答案:

答案 0 :(得分:3)

在您的示例中,编译器添加了>: 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
       ^

答案 1 :(得分:2)

如果对我无用,它看起来无害。 x想要的类型不存在,因此您无法将其传递给方法。我认为,无害无用应该是编译时错误是一个品味问题。

如果你看看x实际上做了什么,它就会反编译:

public java.lang.Object x(java.lang.Object);
  Code:
   0:   aload_1
   1:   areturn

这正是身份方法应该做的事情(加载参数而不管类型,返回它)。你可以用更少的代码编写等价的东西:

trait AbstractType { type T }
class Useless extends AbstractType { def identity(t: AbstractType#T) = t }

除了没有类型抽象类型#T,所以我们再次无用。

除非我遗漏了什么。