option - Scala case class update value -
i have case class 2 string members. update second member later, first create instance string , none , later load data class , update second member value.
how can it?
define case class second member var
:
case class stuff(name: string, var value: option[string])
now can create instance of stuff
, modify second value:
val s = stuff("bashan", none) s.value = some("hello")
however, making case classes mutable not idea. should prefer working immutable data structures. instead of creating mutable case class, make immutable, , use copy
method create new instance modified values. example:
// immutable stuff case class stuff(name: string, value: option[string]) val s1 = stuff("bashan", none) val s2 = s1.copy(value = some("hello")) // s2 now: stuff("bashan", some("hello"))
Comments
Post a Comment