Scala:类型别名与上限的类型匹配
Scala: Type matching of a type alias with upper bound
我的问题很简单
为什么这段代码会出错,
abstract class A {
type T <: A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
// Exiting paste mode, now interpreting.
<console>:16: error: type mismatch;
found : A.Inner
required: A.this.T
def toT: T = new A.Inner(this)
^
而这段代码没有?
abstract class A {
type T = A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
// Exiting paste mode, now interpreting.
defined class A
defined object A
A.Inner <: A.Inner
。不是吗?
此处应使用下限:
abstract class A {
type T >: A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
只有当T
是A.Inner
的祖先时,A.Inner才能转换为T
。我们使用下限来限制 T
是 A.Inner
.
的祖先
我的问题很简单
为什么这段代码会出错,
abstract class A {
type T <: A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
// Exiting paste mode, now interpreting.
<console>:16: error: type mismatch;
found : A.Inner
required: A.this.T
def toT: T = new A.Inner(this)
^
而这段代码没有?
abstract class A {
type T = A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
// Exiting paste mode, now interpreting.
defined class A
defined object A
A.Inner <: A.Inner
。不是吗?
此处应使用下限:
abstract class A {
type T >: A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
只有当T
是A.Inner
的祖先时,A.Inner才能转换为T
。我们使用下限来限制 T
是 A.Inner
.