[英]scala TypeTag of generic subclass
I have a case class Document with a generic type Parameter. 我有一个带有通用类型Parameter的案例类Document。 Now I want get at runtime the TypeParamater types. 现在,我想在运行时获取TypeParamater类型。
I did not found a way. 我没有找到办法。
def toHtml[T <: DocumentType](doc: Document[T])(variableSubstitution: String => String ): String = {
doc match {
case _ : Document[UnStructured] =>
case _ => //
}
}
My Document Class: 我的文件类别:
final case class Document[T <: DocumentType](chapterList: SortedSet[_ <: ChapterTrait[T]],
uniqueId: UUID = UUID.randomUUID(),
createDate: DateTime = DateTime.now(),
lastEdit: DateTime = DateTime.now(),
title: String = "",
version: Double = 1.0,
designSetting: DocumentDesignSettingTrait = DocumentDesignSetting(),
paperFormat: PaperFormat = A4,
headerList: List[PageMark] = Nil,
footerList: List[PageMark] = Nil,
preContent: PreContent = PreContent(),
postContent: PostContent = PostContent(),
author: Author = Author() ) {
}
Now the types for document 现在文件的类型
sealed abstract class DocumentType
sealed abstract class StructuredDocumentType extends DocumentType
sealed abstract class SemiStructured extends StructuredDocumentType
//case object SemiStructured extends SemiStructured
sealed abstract class Structured extends StructuredDocumentType
//case object Structured extends Structured
sealed trait UnStructured extends DocumentType
//case object UnStructured extends DocumentType
I want now find out which type parameter is bounded at doc. 现在,我想找出哪种类型的参数在doc范围内。
Someone any indea? 有个人想法吗?
I'm newish to Scala but I think what you're looking for in your match is something like the following: 我是Scala的新手,但我认为您在比赛中寻找的东西如下:
import scala.reflect.runtime.universe.{TypeTag, typeOf}
def toHtml[T <: DocumentType](doc: Document[T])(variableSubstitution: String => String )(implicit tag: TypeTag[T]): String = typeOf[T] match {
case t if t =:= typeOf[UnStructured] => // ...
case t if t =:= typeOf[SemiStructured] => // ...
case _ => // ...
}
I've had lots of luck using the reflection API when I've programmed with generics in the past. 过去使用泛型进行编程时,我使用反射API感到很幸运。 Let me know if this helps! 让我知道这是否有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.