scala - How to map HList to List[Type], List[TypeTag], or List[String] -
scala - How to map HList to List[Type], List[TypeTag], or List[String] -
i playing around scala repl , bind method. unfortunately takes classtag erases types type information, e.g. list[int]
becomes list[_]
. want pass hlist repl wrapper , type string can pass bind method. have map hlist list of strings.
def extract extends (tuple2[string, t] ~>> string) { def apply[t](value: tuple2[string, t]) = typeof[t].tostring }
the code above not working. 1 thing cannot utilize tuple2. should not hard solve that. however, typeof[t] requires implicit typetag. thought how can doe this? show
help out?
thanks help.
try this,
scala> :paste // entering paste mode (ctrl-d 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, 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
scala shapeless hlist
Comments
Post a Comment