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

Popular posts from this blog

C# random value from dictionary and tuple -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -