简体   繁体   中英

scala macro - how to expand an Array[TermName] to HList concatenation

Suppose I have a case class

case class(id: Option[Long], name: String, gender: String)

Then I got the field names via compile-time reflect

val names = ... // Array(TermName(id), TermName(name), TermName("gender"))

How could I transform those names to id :: name :: gender :: HNil (which has type Option[Long] :: String :: String :: HNil] )

That is say I am looking for things like q"..$name" in quasiquote but expand names with :: instead of ,

Seems could be done like this

   def hlistConcat[T: Liftable ](elems: Iterable[T]) = {
      val HNil = q"HNil": Tree
      elems.toList.reverse.foldLeft(HNil) { (list, c) =>
        q"$c :: $list"
      }
    }

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