With 无形的 https://github.com/milessabin/shapeless您可以对案例类进行抽象。
1. 手动抽象案例类
如果你假设每个id
is a Long
并且是案例类的第一个参数,它可能如下所示:
scala> import shapeless._, ops.hlist.{IsHCons, Prepend}
import shapeless._
import ops.hlist.{IsHCons, Prepend}
scala> trait Copy[A <: Copy[A]] { self: A =>
| def copyWithId[Repr <: HList, Tail <: HList](l: Long)(
| implicit
| gen: Generic.Aux[A,Repr],
| cons: IsHCons.Aux[Repr,Long,Tail],
| prep: Prepend.Aux[Long :: HNil,Tail,Repr]
| ) = gen.from(prep(l :: HNil, cons.tail(gen.to(self))))
| }
defined trait Copy
scala> case class Foo(id: Long, s: String) extends Copy[Foo]
defined class Foo
scala> Foo(4L, "foo").copyWithId(5L)
res1: Foo = Foo(5,foo)
It might also be possible in a cleaner way; I'm not very proficient at shapeless programming yet. And I'm pretty sure it's also possible to do it for case classes with any type of id
in any position in the parameter list. See paragraph 2 below.
您可能希望将此逻辑封装在可重用的类型类中:
scala> :paste
// Entering paste mode (ctrl-D to finish)
import shapeless._, ops.hlist.{IsHCons, Prepend}
sealed trait IdCopy[A] {
def copyWithId(self: A, id: Long): A
}
object IdCopy {
def apply[A: IdCopy] = implicitly[IdCopy[A]]
implicit def mkIdCopy[A, Repr <: HList, Tail <: HList](
implicit
gen: Generic.Aux[A,Repr],
cons: IsHCons.Aux[Repr,Long,Tail],
prep: Prepend.Aux[Long :: HNil,Tail,Repr]
): IdCopy[A] =
new IdCopy[A] {
def copyWithId(self: A, id: Long): A =
gen.from(prep(id :: HNil, cons.tail(gen.to(self))))
}
}
// Exiting paste mode, now interpreting.
import shapeless._
import ops.hlist.{IsHCons, Prepend}
defined trait IdCopy
defined object IdCopy
scala> def copy[A: IdCopy](a: A, id: Long) = IdCopy[A].copyWithId(a, id)
copy: [A](a: A, id: Long)(implicit evidence$1: IdCopy[A])A
scala> case class Foo(id: Long, str: String)
defined class Foo
scala> copy(Foo(4L, "foo"), 5L)
res0: Foo = Foo(5,foo)
如果这对您很重要,您仍然可以将 copyWithId 方法放入案例类可以扩展的特征中:
scala> trait Copy[A <: Copy[A]] { self: A =>
| def copyWithId(id: Long)(implicit copy: IdCopy[A]) = copy.copyWithId(self, id)
| }
defined trait Copy
scala> case class Foo(id: Long, str: String) extends Copy[Foo]
defined class Foo
scala> Foo(4L, "foo").copyWithId(5L)
res1: Foo = Foo(5,foo)
重要的是,通过使用上下文边界或隐式参数,将类型类实例从使用站点传播到需要它的位置。
override def createAndFetch(entity: E)(implicit copy: IdCopy[E]): Future[Option[E]] = {
val insertQuery = tableQuery returning tableQuery.map(_.id)
into ((row, id) => row.copyWithId(id))
db.run((insertQuery += entity).flatMap(row => findById(row.id)))
}
2. 使用镜头
无形还提供lenses https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0#boilerplate-free-lenses-for-arbitrary-case-classes您可以将其用于此目的。这样您就可以更新id
任何具有某些特征的案例类的字段id
field.
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed trait IdCopy[A,ID] {
def copyWithId(self: A, id: ID): A
}
object IdCopy {
import shapeless._, tag.@@
implicit def mkIdCopy[A, ID](
implicit
mkLens: MkFieldLens.Aux[A, Symbol @@ Witness.`"id"`.T, ID]
): IdCopy[A,ID] =
new IdCopy[A,ID] {
def copyWithId(self: A, id: ID): A =
(lens[A] >> 'id).set(self)(id)
}
}
def copyWithId[ID, A](a: A, elem: ID)(implicit copy: IdCopy[A,ID]) = copy.copyWithId(a, elem)
// Exiting paste mode, now interpreting.
defined trait IdCopy
defined object IdCopy
copyWithId: [ID, A](a: A, elem: ID)(implicit copy: IdCopy[A,ID])A
scala> trait Entity[ID] { def id: ID }
defined trait Entity
scala> case class Foo(id: String) extends Entity[String]
defined class Foo
scala> def assignNewIds[ID, A <: Entity[ID]](entities: List[A], ids: List[ID])(implicit copy: IdCopy[A,ID]): List[A] =
| entities.zip(ids).map{ case (entity, id) => copyWithId(entity, id) }
assignNewIds: [ID, A <: Entity[ID]](entities: List[A], ids: List[ID])(implicit copy: IdCopy[A,ID])List[A]
scala> assignNewIds( List(Foo("foo"),Foo("bar")), List("new1", "new2"))
res0: List[Foo] = List(Foo(new1), Foo(new2))
请注意方法中如何assignNewIds
where copyWithId
使用类型类的实例IdCopy[A,ID]
作为隐式参数请求。这是因为copyWithId
需要一个隐式实例IdCopy[A,ID]
使用时要在范围内。您需要从使用站点传播隐式实例,您可以在其中使用具体类型,例如Foo
,沿着调用链一直到哪里copyWithId
叫做。
您可以将隐式参数视为方法的依赖项。如果方法具有类型的隐式参数IdCopy[A,ID]
,当您调用它时,您需要满足该依赖关系。通常,这也会对调用它的方法产生同样的依赖。