简体   繁体   English

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

[英]Constructor result type is not <:< class type on class with type parameter

According to <:< , the result type of a class's constructor is not that of the class, if the class has a type parameter. 根据<:< ,如果类具有类型参数,则该类的构造函数的结果类型不是该类的结果类型。 Here's a small program to demonstrate: 这是一个演示的小程序:

// 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"
}

Here's the output: 这是输出:

ClassWithTypeParam[String]
(x: scala.Int)ClassWithTypeParam[A]
List(value x)
ClassWithTypeParam[A]

false

It also fails if I call typeOf rather than weakTypeOf . 如果我调用typeOf而不是weakTypeOf它也会失败。 The last line is true when I try this on classes that don't have type parameters. 当我在没有类型参数的类上尝试此操作时,最后一行是true

Do I need to call .substituteTypes or .typeSignatureIn ? 我需要调用.substituteTypes.typeSignatureIn吗? If so, what belongs in the arguments to these methods? 如果是这样,这些方法的参数属于什么? I haven't found much documentation on them. 我还没有找到太多关于它们的文档。

使用typeSignatureIn而不是typeSignature来获得说明符号所有者确切类型的类型信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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