scala 在不同类型的case类之间安全地复制字段

z9zf31ra  于 2023-10-18  发布在  Scala
关注(0)|答案(2)|浏览(116)

假设您有如下的case类

case class Test1(a:String,b:Int,c:Char)

case class Test2(a:String,b:Int)

然后使用以下变量示例化类

val test1 = Test1("first",2,'3')

val test2 = Test2("1st",20)

有没有一种方法可以使用.copy方法(或其他方法),将Test 2中的变量应用到Test1中,类似于

val test3 = test1.copy(test2) //Note this isn't valid scala code
// Result should be ("1st",20,'3')

如果这在纯scala中是不可能的,那么在Shapeless 1/2中如何实现呢(目前的代码是在Shapeless 1中,但是我们计划在某个时候升级到Shapeless 2)

lf5gs5x2

lf5gs5x21#

在shapeless 2.0.0中,这可以像这样完成,

scala> import shapeless._
import shapeless._

scala> case class Test1(a: String, b: Int, c: Char)
defined class Test1

scala> case class Test2(a: String, b: Int)
defined class Test2

scala> val test1 = Test1("first", 2, '3')
test1: Test1 = Test1(first,2,3)

scala> val test2 = Test2("1st", 20)
test2: Test2 = Test2(1st,20)

scala> val test1Gen = Generic[Test1]
test1Gen: ... = $1$$1@6aac621f

scala> val test2Gen = Generic[Test2]
test2Gen: ... = $1$$1@5aa4954c

scala> val test3 = test1Gen.from(test2Gen.to(test2) :+ test1.c)
test3: Test1 = Test1(1st,20,3)

请注意,这是对每个case类中字段顺序的假设,而不是使用字段标签信息。如果你有多个相同类型的字段,这可能容易出错:类型可能会对齐,但潜在语义可能会改变。
我们可以通过使用shapeless的LabelledGeneric来解决这个问题。LabelledGeneric将case类的值Map到无形的可扩展记录,除了捕获字段值的类型外,还通过相应Scala Symbol的单例类型将字段名编码到类型中。通过一点额外的基础设施(我将很快添加到shapeless 2.1.0),这允许我们使用最少的样板文件安全地在case类之间Map,

import shapeless._, record._, syntax.singleton._, ops.hlist.Remove

/**
 * This will be in shapeless 2.1.0 ...
 *
 * Permute the elements of the supplied `HList` of type `L` into the same order
 * as the elements of the `HList` of type `M`.
 */
trait Align[L <: HList, M <: HList] extends (L => M) {
  def apply(l: L): M
}

object Align {
  def apply[L <: HList, M <: HList]
    (implicit alm: Align[L, M]): Align[L, M] = alm

  implicit val hnilAlign: Align[HNil, HNil] = new Align[HNil, HNil] {
    def apply(l: HNil): HNil = l
  }

  implicit def hlistAlign[L <: HList, MH, MT <: HList, R <: HList]
    (implicit
      select: Remove.Aux[L, MH, (MH, R)],
      alignTail: Align[R, MT]): Align[L, MH :: MT] = new Align[L, MH :: MT] {
    def apply(l: L): MH :: MT = {
      val (h, t) = l.removeElem[MH]
      h :: alignTail(t)
    }
  }
}

/**
 * This, or something like it, will be in shapeless 2.1.0 ...
 *
 * Utility trait intended for inferring a field type from a sample value and
 * unpacking it into its key and value types.
 */
trait Field {
  type K
  type V
  type F = FieldType[K, V]
}

object Field {
  def apply[K0, V0](sample: FieldType[K0, V0]) =
    new Field { type K = K0; type V = V0 }
}

object OldWineNewBottle {
  case class From(s1: String, s2: String)
  case class To(s2: String, i: Int, s1: String)

  val from = From("foo", "bar")

  val fromGen = LabelledGeneric[From]
  val toGen = LabelledGeneric[To]

  // Define the type of the i field by example
  val iField = Field('i ->> 0)

  val align = Align[iField.F :: fromGen.Repr, toGen.Repr]

  // extend the fields of From with a field for 'i', permute into
  // the correct order for To and create a new instance ...
  val to = toGen.from(align('i ->> 23 :: fromGen.to(from)))

  assert(to == To("bar", 23, "foo"))
}
cczfrluj

cczfrluj2#

+1无定形
将Scala case class对象复制到另一个case class,并在末尾添加额外的字段:

import shapeless._
  import syntax.singleton._
  case class A(a: Int, b: Int, c: Int, d: Int, e: Int)
  case class B(a: Int, b: Int, c: Int, d: Int, e: Int, z: Int)
  val agen = LabelledGeneric[A]
  val bgen = LabelledGeneric[B]
  val a = A(1, 2, 3, 4, 5)
  val b = bgen.from(agen.to(a) :+ ('z ->> 999))

将Scala case类对象复制到另一个case类对象,并在开头添加额外的字段:

import shapeless._
  import syntax.singleton._
  case class A(a: Int, b: Int, c: Int, d: Int, e: Int)
  case class B(x: Int, y: Int, z: Int, a: Int, b: Int, c: Int, d: Int, e: Int)
  val agen = LabelledGeneric[A]
  val bgen = LabelledGeneric[B]
  val a = A(1, 2, 3, 4, 5)
  val b = bgen.from(('x ->> 777) +: ('y ->> 888) +: ('z ->> 999) +: agen.to(a))

基本上,这是迄今为止我看到的避免列出所有字段的最佳方法。

B.apply(A a) { a = a.a; b = a.b, c = a.c, etc… }

相关问题