I have the following types defined:
sealed trait Refreshable {
val expireAt: Instant
val version: Int
}
case class A(id: Int, expireAt: Instant, version: Int) extends Refreshable
case class B(name: String, expireAt: Instant, version: Int) extends Refreshable
I would like to define a method refresh
that updates two fields on these types, but I cannot find the implicits I need for this. It seems like I should be able to do something like this with shapeless ?
def refresh[T <: RefreshableClass])(v: T)(implicit ev: LabelledGeneric[T]) = {
val gen = ev.from(v)
gen.updateWith('expire)(_.plus(...)).updateWith('version)(_+1)
}
Make your trait sealed.
sealed trait Refreshable {
val expireAt: Instant
val version: Int
}
With Typelevel Scala the following variant works:
def refresh[T <: Refreshable, R <: HList](v: T)(implicit
generic: LabelledGeneric.Aux[T, R],
modifier: Modifier.Aux[R, Symbol @@ "version", Int, Int, R],
modifier1: Modifier.Aux[R, Symbol @@ "expireAt", Instant, Instant, R]): R = {
val rec = generic.to(v)
val rec1 = modifier(rec, _ + 1)
modifier1(rec1, _.plus(1L, ChronoUnit.MILLIS))
}
With Lightbend Scala (ie standard Scala)
def refresh[T <: Refreshable, R <: HList](v: T)(implicit
generic: LabelledGeneric.Aux[T, R],
modifier: Modifier.Aux[R, Witness.`'version`.T, Int, Int, R],
modifier1: Modifier.Aux[R, Witness.`'expireAt`.T, Instant, Instant, R]): R = {
val rec = generic.to(v)
val rec1 = modifier(rec, _ + 1)
modifier1(rec1, _.plus(1L, ChronoUnit.MILLIS))
}
If you prefer to work with updateWith
this is also possible:
def refresh[T <: Refreshable, R <: HList](v: T)(implicit
generic: LabelledGeneric.Aux[T, R],
modifier: Modifier.Aux[R, Witness.`'version`.T, Int, Int, R],
modifier1: Modifier.Aux[R, Witness.`'expireAt`.T, Instant, Instant, R],
selector: Selector.Aux[R, Witness.`'version`.T, Int],
selector1: Selector.Aux[R, Witness.`'expireAt`.T, Instant]): R = {
val rec = generic.to(v)
rec.updateWith('expireAt)(_.plus(1L, ChronoUnit.MILLIS))
.updateWith('version)(_ + 1)
}
Normally you don't need to specify type parameters T
and R
, they should be inferred:
refresh(A(1, Instant.now, 1)) //1 :: 2017-09-20T18:53:34.039Z :: 2 :: HNil
refresh(B("a", Instant.now, 1)) // a :: 2017-09-20T18:53:34.074Z :: 2 :: HNil
But if you do need you can specify them:
refresh[A, Record.`'id -> Int, 'expireAt -> Instant, 'version -> Int`.T](A(1, Instant.now, 1))
refresh[B, Record.`'name -> String, 'expireAt -> Instant, 'version -> Int`.T](B("a", Instant.now, 1))
Probably simplest way is to use shapeless lenses. First, some imports:
import java.time._
import shapeless._
Inheritance from common trait is not required if fields match, so for the purpose of this example I'll remove it:
case class A(id: Int, expireAt: Instant, version: Int)
case class B(name: String, expireAt: Instant, version: Int)
Create paths selecting the product element labelled expireAt / version
val (expirePath, versionPath) = (^.expireAt, ^.version)
Write your method accepting lens factories for given paths:
def refresh[T](t: T)(implicit
expireL: expirePath.Lens[T, Instant],
versionL: versionPath.Lens[T, Int]): T = {
val t1 = expireL().set(t)(Instant.now())
val t2 = versionL().modify(t1)(_ + 1)
t2
}
Use it:
println(refresh(A(1, null, 0)))
println(refresh(B("The B", null, 4)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.