简体   繁体   中英

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 . The last line is true when I try this on classes that don't have type parameters.

Do I need to call .substituteTypes or .typeSignatureIn ? If so, what belongs in the arguments to these methods? I haven't found much documentation on them.

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

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.

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