gpt4 book ai didi

scala - Shapeless:使用 Record 批量更新

转载 作者:行者123 更新时间:2023-12-01 07:50:16 25 4
gpt4 key购买 nike

我需要在没有所有copy 代码的情况下修改案例类的多个字段。看起来 shapeless 是一个不错的选择。

根据 example ,我可以使用 lens 这种形式:

lensA ~ lensB ~ lensC set(something)(valA, valB, valC)

这很好。然而,就我而言,嵌套字段不是我最关心的问题(我相信它会 :-< )。所以 lens 解决方案与以下内容几乎相同:

something.copy(a = valA, b = valB, c = valC)

我想指出的一件事是,并非所有的修改都必然发生。在我的伪案例中,我可能会根据上下文中的某些 if/else 更新所有 a,b,c 或其中的一些,或者不更新。

因此,使用这种用法的Record正是我所需要的:

someHList + ('a ->> valA) + ('b ->> valB) + ('c ->> valC)

甚至最终:

Seq(
'a ->> valA,
'b ->> valB,
'c ->> valC
).fold(someHList)(_ + _)

根据我的编译器这是不可能的(产生类型不匹配错误)。

我知道这种用法只存在于我的想象中,不存在于文档中。但是,我真的很感激使用 Recordlens 或其他任何方法来解决我的问题的正确方法。也欢迎任何其他优雅的方式!

谢谢!

最佳答案

已经有 update single field operation +通过 Updater操作提供者,您唯一需要做的就是通过 some fold operation 应用它

所以你可以这样写

import shapeless._
import shapeless.ops.hlist.LeftFolder
import shapeless.ops.record.Updater
import syntax.singleton._
import record._

object updateAll extends Poly2 {
implicit def updateOne[L <: HList, F](implicit update: Updater[L, F]) = at[L, F]((l, f) => update(l, f))
}

implicit class UpdateAllOps[L <: HList](record: L) {
def ++>[U <: HList](updates: U)(implicit fl: LeftFolder[U, L, updateAll.type]): fl.Out =
fl(updates, record)
}

现在有

val rec = 'x ->> "Old" :: 'y ->> 1 :: HNil
val upd = 'z ->> true :: 'x ->> "New" :: HNil

你可以验证

rec ++> upd

是一样的

'x ->> "New" :: 'y ->> 1 :: 'z ->> true :: HNil

但重要的是要注意

val str = "New".asInstanceOf[String with Serializable]
rec ++> ('x ->> str :: HNil)

会产生类似

的结果
'x ->> "Old" :: y ->> 1 :: 'x -> "New" :: HNil

所以你应该非常小心你的类型,除非你定义你自己的 Updater

替代品

关于scala - Shapeless:使用 Record 批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com