[Q] [1.7.x] Patching -- How can I use a Option[F[T]]
as Option[Option[T]]
?
#697
-
The code below works in version 1.6, but it does not work in version 1.7. How can this be fixed? import io.scalaland.chimney.dsl._
import io.scalaland.chimney.Transformer
case class Clearable[T](value: Option[T])
object Clearable {
def someValueUpdate[T](value: T): Option[Clearable[T]] = Some(Clearable.set(value))
def someCleanup[T]: Option[Clearable[T]] = Some(Clearable.clear)
def set[T](value: T): Clearable[T] = Clearable(Some(value))
def clear[T]: Clearable[T] = Clearable(Option.empty[T])
}
case class Person(id: Int, firstname: String, lastname: String, note: Option[String], email: Option[String], address: Option[String])
case class PersonUpdate(
note: Option[Clearable[String]] = None,
email: Option[Clearable[String]] = None,
address: Option[Clearable[String]] = None
)
val p1 = Person(0, "Joe", "Doe", Some("note"), Some("em@ail.com"), Some("Example Street 42"))
given clearableTransformer[T]: Transformer[Clearable[T], Option[T]] = (src: Clearable[T]) => src.value
val updated = p1.patchUsing(PersonUpdate(note = Clearable.someValueUpdate("new note"), address = Clearable.someCleanup))
println(updated)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can either:
https://scastie.scala-lang.org/MateuszKubuszok/Z01iEi49QTqKjmju3afPcg |
Beta Was this translation helpful? Give feedback.
You can either:
Patcher
to updateOption[T]
withOption[Clearable[T]]
- it's quick and easy, but hardcodes all the typesClearable[T]
is like anOption[T]
-Option[Option[T]]
have the semantics that you seem to want here - it a bit more work but it's more flexiblehttps://scastie.scala-lang.org/MateuszKubuszok/Z01iEi49QTqKjmju3afPcg