[英]How to specify scala type parameter constraint when also specifying TypeTag
I can capture the type of some type parameter of a class like so: 我可以像这样捕获类的某些类型参数的类型:
import scala.reflect.runtime.universe._
case class X[T:TypeTag]() {
val typ = typeOf[T]
}
But how can I do the same if I want to enforce a type constraint: 但是,如果我要强制执行类型约束,该怎么做:
trait SomeClass
case class X[T<:SomeClass]() {
val typ = typeOf[T]
}
This doesn't work: 这不起作用:
case class X[T<:SomeClass with TypeTag]() { // TypeTag takes parameters
val typ = typeOf[T]
}
neither does: 也没有:
case class X[T<:SomeClass with TypeTag[T]]() {
val typ = typeOf[T]
}
X[String]()// type arguments [String] do not conform to method apply's type parameter bounds [T <: SomeClass with reflect.runtime.universe.TypeTag[T]]
scala> case class X[T <: SomeClass : TypeTag]() {
val typ = typeOf[T]
}
defined class X
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.