繁体   English   中英

构造函数结果类型不是带有类型参数的类上的<:<类类型

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM