[英]How to map HList to List[Type], List[TypeTag], or List[String]
I am playing around with the Scala REPL and its bind method. 我在玩Scala REPL及其绑定方法。 Unfortunately it takes a ClassTag which erases types some type information, eg List[Int]
becomes List[_]
. 不幸的是,它需要一个ClassTag来擦除类型的一些类型信息,例如List[Int]
变为List[_]
。 So I want to pass an HList to my REPL wrapper and get the type as a string which I can pass to the bind method. 所以我想将HList传递给REPL包装器,并以字符串形式获取类型,然后将其传递给bind方法。 For this I have to map an HList to a List of strings. 为此,我必须将HList映射到字符串列表。
def extract extends (Tuple2[String, T] ~>> String) {
def apply[T](value: Tuple2[String, T]) = typeOf[T].toString
}
The code above is not working. 上面的代码不起作用。 For one thing I cannot use Tuple2. 一方面,我不能使用Tuple2。 It should not be too hard to solve that. 解决这个问题应该不太困难。 However, typeOf[T] requires an implicit TypeTag. 但是,typeOf [T]需要隐式TypeTag。 Any idea how I can doe this? 知道我该怎么做吗? Could show
help out? 可能show
帮帮忙?
Thanks for any help. 谢谢你的帮助。
Try something like this, 试试这个
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.runtime.universe._
import shapeless._, poly._
object extract extends Poly1 {
implicit def caseT[T: TypeTag] = at[(String, T)](_ => typeOf[T].toString)
}
// Exiting paste mode, now interpreting.
import scala.reflect.runtime.universe._
import shapeless._
import poly._
defined object extract
scala> val l = ("foo", 23) :: ("bar", true) :: ("baz", 2.0) :: HNil
l: ... = (foo,23) :: (bar,true) :: (baz,2.0) :: HNil
scala> l map extract
res0: String :: String :: String :: HNil = Int :: Boolean :: Double :: HNil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.