- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个案例类:
case class Foo(num: Int, str: String, bool: Boolean)
现在我还有一个简单的包装器:
sealed trait Wrapper[T]
case class Wrapped[T](value: T) extends Wrapper[T]
(以及其他一些在这里不重要的 Wrapper 实现)
val genFoo = Generic[Foo]
(在我的真实代码中,我使用 LabelledGeneric 以免丢失字段名称)
Generic.Aux[Foo, Int :: String :: Boolean :: HNil]
(当与 LabelledGeneric 一起使用时,定义要复杂得多,但本质上是一样的)
type WrappedHlist = Wrapper[Int] :: Wrapper[String] :: Wrapper[Boolean] :: HNil
然后我可以使用这个类型定义来生成一个 Circe 编码器/解码器(我已经为 Wrapper 类型提供了必要的编码器/解码器)。
1 Câu trả lời
嗯,这可以通过一些类型类、依赖路径的类型和 Aux 模式来实现:
trait WrapperHelper[In] {
type Out
def wrap(i: In): Out
}
object WrapperHelper {
type Aux[I, O] = WrapperHelper[I] { type Out = O }
implicit val nilWH: WrapperHelper.Aux[HNil, HNil] = new WrapperHelper[HNil] {
type Out = HNil
def wrap(i: HNil): HNil = i
}
implicit def hconsWH[H, TI <: HList, TO <: HList](
implicit
tailWH: WrapperHelper.Aux[TI, TO]
): WrapperHelper.Aux[H :: TI, Wrapper[H] :: TO] = new WrapperHelper[H :: TI] {
type Out = Wrapper[H] :: TO
def wrap(i: H :: TI): Wrapper[H] :: TO = i match {
case head :: tail => Wrapped(head) :: tailWH.wrap(tail)
}
}
}
def wrap[I](i: I)(implicit wh: WrapperHelper[I]): wh.Out = wh.wrap(i)
HNIl 被视为一种特殊情况,其中 In = Out。对于其他一切……您仍然必须递归地映射类型。虽然不漂亮,但这应该做你想做的:
@ wrap(Generic[Foo].to(Foo(1, "", true)))
res7: Wrapper[Int] :: Wrapper[String] :: Wrapper[Boolean] :: HNil = Wrapped(1) :: Wrapped("") :: Wrapped(true) :: HNil
假设您想提供一些自定义编码/解码逻辑,您应该修改签名
implicit def hconsWH[H, TI <: HList, TO <: HList](
implicit
tailWH: WrapperHelper.Aux[TI, TO]
): WrapperHelper.Aux[H :: TI, Wrapper[H] :: TO]
像
implicit def hconsWH[H, TI <: HList, TO <: HList](
implicit
thatThingINeedToLiftAToWrapperA: Encoder[A], // whatever is needed to lift A => Wrapper[A]
tailWH: WrapperHelper.Aux[TI, TO]
): WrapperHelper.Aux[H :: TI, Wrapper[H] :: TO]
即使您不需要实现,在这里定义 typeclass 仍然很有用,只是为了提供一些可以为您解析类型的隐式证据。
关于scala - 如何定义一个 HList 类型,但基于另一个 HList 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65201029/
在学习无形时,我想知道为什么这不能编译: def someHList[H <: HList]: H = HNil 因为 HNil 对象扩展了扩展 HList 的 HNil 特性? 在返回一些 HLis
偶.head不起作用。 我需要做哪些改变才能使这项工作发挥作用? import shapeless._ import HList._ import Nat._ scala> case class Fo
我正在编写一个通用表格查看器,它应该能够显示任何类型的 Seq[Row]其中 Row B.sort(hlistNthLens[R,nat(ci)]))(tab.hdrs(ci).toString))
鉴于以下 case class A(value:Int) case class B(value:String) val h:Option[A] :: A :: Option[B] :: Option[
我正在寻找一种将两个 HList 压缩在一起的方法。第一个是从以其通用表示形式转换的案例类生成的,第二个是手动定义为 Nat 的 HList。 因此,我期望一个元组(或 2 成员 HList)包含案例
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
我正在学习 shapeless,目前我正在尝试创建一个执行以下操作的函数:给定一个 HList 类型,它返回 None 的 HList,Option 类型对应给定 HList 类型。 例如: crea
我想创建相当于: def toTupleN[A1, ..., AN, L <: HList](l: L): TupleN[A1, ..., AN] 代码使用 toTupleN只有当只有一个 N 时才应
这个问题来源于我之前的问题:What does HList#foldLeft() return? 我有这个场景: class Cursor { } trait Column[T] { def r
例如,如果我有一个 HList: HList>> list = ... 有没有办法将每个元素应用于柯里化(Currying)函数: F>> f = ... 这样我就能以某种方式得到 D ? 此外,如果
我正在尝试实现获取第一个元素的通用函数: import shapeless.ops.hlist.IsHCons import shapeless.{Generic, HList} object App
假设我们有这个: def join[A, B](a: A, b: B) def join[A, B, C](a: A, b: B, c: C) // etc 基本上有很多用于最多 20 个类型参数的重
我正在尝试以下类(class) import shapeless._ import syntax.std.tuple._ class TestedClass[HL](nodes: HL) {
下面的代码似乎很明显可以编译和运行 case class Pair(a: String, b: Int) val pairGen = Generic[Pair] object size extends
我有一个复杂的类型层次结构,但要分解它有两个基本特征:Convertable和 Conversion[A conv.convert(automaton)) } override def conv
假设我有一个像这样的类型类: trait Select[A] { def select(selector: String): Set[A] } typeclass 提供了“给定一个选择器字符串,给
我想做这样的事情: def run(subjects: List[Subject]) = { val configs = compute() subjects.map(s => configs
我一直在尝试使用 HList创建记录。 我一直在使用 HList-GHCSyntax 中定义的运算符. 到目前为止它工作得很好,让我可以写这样的东西: myRecord = (param1 .=.
HList package是基于现在古老的 Haskell 技术。一个简单的问题是:考虑到过去 8 年 Haskell/GHC 开发的所有精彩新特性,“现代”HList 的构建方式会非常不同吗?我意识
我试图解决 this problem与无形。但是由于某种原因我无法在 HList 上映射.我会让代码不言自明。 import shapeless._ import HList._ case class
Tôi là một lập trình viên xuất sắc, rất giỏi!