我有一個像這樣的更高種類的資料型別
case class Foo[F[_] : Functor](a: F[Int], b: F[Double])
以及一個用某種字串標記每個成員的值(例如 CSV 列的名稱):
val colNames = new Foo[Const[String, *]](a = Const("AAA"), b = Const("BBB"))
現在我需要以某種方式轉換colNames為Seq[String]. 當然有標準的 scala 方法productIterator,但它只回傳Iterator[Any].
使用 shapeless 我可以獲得一個 HList ,其中串列中的每種型別都是Const[String, _]:
val colNamesHlist: Const[String, _] :: Const[String, _] :: HNil = Generic[Foo[Const[String, *]]].to(colNames)
但是我怎樣才能把它變成一個常規串列呢?
我也對任何不涉及無形的想法持開放態度,盡管我認為這是無法避免的。
uj5u.com熱心網友回復:
你可以這樣做:
val colNameList: List[String] =
Generic[Foo[Const[String, *]]]
.to(colNames)
.toList[Const[String, A] forSome { type A }]
.map(_.getConst)
您可以看到這里運行的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424403.html
上一篇:在Scala的可變串列中合并物件
