[英]Shapeless: is there a way to get Hlist type from a case class or a tuple
I'm totally new to shapeless. 我完全没有变形。 I create a case class from a list as following: 我从列表中创建一个case类,如下所示:
val list = Seq(Some(1), Some(1.0), ...)
val y =
list
.toHList[Option[Int]::Option[Double]::Option[Int]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::Option[Double]::HNil]
val z = y.get.tupled
val aa = YieldVariables.tupled(z)
It works well but I'm wondering if there is a way not to write all these types in the toHList[Here]
part. 它运作良好,但我想知道是否有办法不在toHList[Here]
部分写入所有这些类型。
So I want to know if something like list.toHList[find the type yourself]
or list.getTypesForHlist
or yet MyCaseClass.getTypesForHlist
that results to Option[Int]::Option[Double]...
exists. 所以我想知道像list.toHList[find the type yourself]
或者list.getTypesForHlist
或者是导致Option[Int]::Option[Double]...
MyCaseClass.getTypesForHlist
。
For case classes (and tuples, these are also case classes!), use Generic
: 对于case类(和元组,这些也是case类!),使用Generic
:
case class A(i: Int, s: String)
shapeless.Generic[A].to(A(1, "")) // Int :: String :: HNil
This is not possible to do on Seq
. 这在Seq
上是不可能做到的。 Indeed, as soon as you've called the constructor, the information about how many element you passed to that constructor is gone from a type perspective. 实际上,只要您调用了构造函数,就会从类型的角度来看有关传递给该构造函数的元素数量的信息。 Shapeless also has SingletonProductArgs
: a macro for a varargs like syntax that returns an HList
instead of a Seq
. Shapeless还有SingletonProductArgs
:一个类似varargs的宏,它返回HList
而不是Seq
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.