为什么不编译此类型别名(Scala)
我正在尝试让以下代码正常工作:为什么不编译此类型别名(Scala),scala,Scala,我正在尝试让以下代码正常工作: abstract class Vec[Self <: Vec[Self,T], T] { this : Self => def *(y : Self) : Self } abstract class LA[T] { type V <: Vec[V, T] } object typetest2 { def doesntcompile[L <: LA[Double]](x : L#V, y : L#V) : Un
abstract class Vec[Self <: Vec[Self,T], T] {
this : Self =>
def *(y : Self) : Self
}
abstract class LA[T] {
type V <: Vec[V, T]
}
object typetest2 {
def doesntcompile[L <: LA[Double]](x : L#V, y : L#V) : Unit = {
val z = x * y
}
def compiles[V <: Vec[V,_]](x : V, y : V) : Unit = {
val z = x * y
}
}
这是类型检查器的故障还是我做错了什么?虽然我不确定对于这种特殊情况是否有解决办法,但编译器通常无法判断两个路径依赖类型是否相等,即使它们是相同的 通常可以通过添加其他类型参数来解决此类情况:
def compiles2[V1 <: Vec[V1, _], L <: LA[Double]{type V = V1}](x: L#V, y: L#V) = {
val z = x * y
}
def compiles2[V1]一个有趣的问题。我试图让它编译,但失败了。但我发现它也无法编译,因为有一个简单得多的例子:如果您将LA设为非泛型和非抽象,并将type V
替换为class MyDouble(d:Double)扩展了Vec[MyDouble,Double]
,然后甚至删除了doesntcompile
的泛型,并将x
和y
的类型更改为LA#MyDouble
,它确实失败了,我也遇到了几乎相同的编译错误。。。
def compiles2[V1 <: Vec[V1, _], L <: LA[Double]{type V = V1}](x: L#V, y: L#V) = {
val z = x * y
}