[英]Constructor result type is not <:< class type on class with type parameter
根據<:<
,如果類具有類型參數,則該類的構造函數的結果類型不是該類的結果類型。 這是一個演示的小程序:
// Scala 2.11.1
case class ClassWithTypeParam[A](x: Int)
def main(args: Array[String]) {
val classType = weakTypeOf[ClassWithTypeParam[String]]
val constructorType = classType.decl(termNames.CONSTRUCTOR).asMethod.typeSignature
val MethodType(constructorParams, resultType) = constructorType
println(s"$classType\n$constructorType\n$constructorParams\n$resultType\n")
println(resultType <:< classType) // should print "true"
}
這是輸出:
ClassWithTypeParam[String]
(x: scala.Int)ClassWithTypeParam[A]
List(value x)
ClassWithTypeParam[A]
false
如果我調用typeOf
而不是weakTypeOf
它也會失敗。 當我在沒有類型參數的類上嘗試此操作時,最后一行是true
。
我需要調用.substituteTypes
或.typeSignatureIn
嗎? 如果是這樣,這些方法的參數屬於什么? 我還沒有找到太多關於它們的文檔。
使用typeSignatureIn而不是typeSignature來獲得說明符號所有者確切類型的類型信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.