My question is simple.
Why does this code make an error,
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)
^
whereas this code does not?
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
. Isn't it?
Here, lower-bound should be used:
abstract class A {
type T >: A.Inner
def toT: T = new A.Inner(this)
}
object A {
class Inner(a: A)
}
Only if T
is ancestor of A.Inner
, then A.Inner can be converted to T
. We use lower-bound to restrict T
is ancestor of A.Inner
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.