为什么可以使用未具体化的路径相关类型定义 class?
Why is it possible to define a class with a unreified path-dependent type?
考虑以下示例:
trait Supe {
type Out <: Supe
def out: Out
}
class Reif1 extends Supe {
type Out = Reif1
override def out: Out = this
}
class Reif2 extends Supe {
type Out >: this.type <: Reif2
override def out: Out = this
}
class Reif1
显然应该有效。因为 type Out
被具体化并成为类型别名
class Reif2
也有效但认真吗? type Out
只定义了 upper/lower 边界,而且边界还不够紧:this.type
是单例类型,Reif2
是 class 类型。那么如果 Reif2
被实例化, Out
到底会是什么样子呢?会是this.type
吗?还是Reif2
?但更大的问题应该是:为什么 scalac 2.12/2.13 允许编译它?
在 Scala(或 DOT 演算 1 2)中,所有类型都是区间。
type Out = Reif1
是(或应该是)type Out >: Reif1 <: Reif1
。
无边界抽象类型 type Out
是 type Out >: Nothing <: Any
。
So what exactly would Out
look like if Reif2
got instantiated?
它将完全保持 type Out >: this.type <: Reif2
val r = new Reif2
import scala.reflect.runtime.universe._
typeOf[r.Out] // App.r.Out
showRaw(typeOf[r.Out]) // TypeRef(SingleType(ThisType(App), TermName("r")), TypeName("Out"), List())
typeOf[r.Out].typeSymbol.isAbstract // true
typeOf[r.Out].typeSymbol.typeSignature // >: Reif2.this.type <: App.Reif2
如果将 Reif2
中的 type Out >: this.type <: Reif2
替换为 type Out = this.type
(或 type Out = Reif2
或 type Out = Supe
),则 isAbstract
将 return false
.
(见抽象类型的应用)
What is the meaning of a type declaration without definition in an object?(看看为什么检查 type T >: L <: U
不是抽象的并不容易)
Concrete classes can have abstract type members #1753
SI-8217 allow abstract type members in objects #4024
Abstract type members are incorrectly forbidden in objects (unless inherited) #8217
Use of abstract type in a concrete class?
Concrete classes with abstract type members
考虑以下示例:
trait Supe {
type Out <: Supe
def out: Out
}
class Reif1 extends Supe {
type Out = Reif1
override def out: Out = this
}
class Reif2 extends Supe {
type Out >: this.type <: Reif2
override def out: Out = this
}
class Reif1
显然应该有效。因为 type Out
被具体化并成为类型别名
class Reif2
也有效但认真吗? type Out
只定义了 upper/lower 边界,而且边界还不够紧:this.type
是单例类型,Reif2
是 class 类型。那么如果 Reif2
被实例化, Out
到底会是什么样子呢?会是this.type
吗?还是Reif2
?但更大的问题应该是:为什么 scalac 2.12/2.13 允许编译它?
在 Scala(或 DOT 演算 1 2)中,所有类型都是区间。
type Out = Reif1
是(或应该是)type Out >: Reif1 <: Reif1
。
无边界抽象类型 type Out
是 type Out >: Nothing <: Any
。
So what exactly would
Out
look like ifReif2
got instantiated?
它将完全保持 type Out >: this.type <: Reif2
val r = new Reif2
import scala.reflect.runtime.universe._
typeOf[r.Out] // App.r.Out
showRaw(typeOf[r.Out]) // TypeRef(SingleType(ThisType(App), TermName("r")), TypeName("Out"), List())
typeOf[r.Out].typeSymbol.isAbstract // true
typeOf[r.Out].typeSymbol.typeSignature // >: Reif2.this.type <: App.Reif2
如果将 Reif2
中的 type Out >: this.type <: Reif2
替换为 type Out = this.type
(或 type Out = Reif2
或 type Out = Supe
),则 isAbstract
将 return false
.
What is the meaning of a type declaration without definition in an object?(看看为什么检查 type T >: L <: U
不是抽象的并不容易)
Concrete classes can have abstract type members #1753
SI-8217 allow abstract type members in objects #4024
Abstract type members are incorrectly forbidden in objects (unless inherited) #8217
Use of abstract type in a concrete class?
Concrete classes with abstract type members