8
假设我有:斯卡拉:使用类型参数或抽象类型,类型限制
class Bounded[A] {
type apply[C <: A] = C
}
这编译:
implicitly[Bounded[Any]#apply[String] =:= String]
这种失败:
type Str = Bounded[Any]#apply[String]
...有:
[error] /home/grant/Workspace/scunits/test/src/main/scala/Box.scala:37: type arguments[String] do not conform to type apply's type parameter bounds [C <: A]
[error] type Str = Bounded[Any]#apply[String]
[error] ^
我尝试使用抽象类型而不是类型参数,结果相同。我发现的唯一解决方法是实例化类型。这将编译:
val boundedAny = new Bounded[Any]
type Str2 = boundedAny.apply[String]
不幸的是,我用不具有性能方面的原因运行时间的情况下,往往幻影类型的工作。
为什么Scala会在这里产生编译错误?有更好的解决方法吗?
感谢您的任何帮助。
更新:除了下面的解决方法,我需要一种方法来覆盖具有抽象类型边界的类型。我这样做,像这样:
object Test {
class AbstractBounded[A] {
type apply[C <: A] <: A
class Workaround[C <: A] {
type go = apply[C]
}
}
class Bounded[A] extends AbstractBounded[A] {
type apply[C <: A] = C
}
type Str = Bounded[Any]#Workaround[String]#go
}
谢谢,这个bug对我来说是很多挫折的源头。 – Grant 2014-10-14 23:38:09