scala - case class modification and design generic way -
code snippets trivialized, sampled, purpose of post.
case class person(firstname:string,lstname:string)
this person class has been used on place in codebase. later requirements changed , deciding add phonenumber
in person case class
e.g.
case class person(firstname:string,lstname:string,phonenumber:string)
again, examples extremely simplified in post. in reality there more interesting things happening. note phonenumber
not option
required field. 1 go , update code that's using person
class, cater new field lastname
. quite tedious when have 100 references of it.
can shapeless in creating more flexible hlist
go vs case class?
the easiest way go provide default value phonenumber
:
case class person(firstname: string, lastname: string, phonenumber: string = "")
alternatively, create companion object , implement apply()
method both cases, i.e. , without phonenumber
. if decide take approach, , you're using case class in pattern matches, might want implement corresponding unapply()
methods.
Comments
Post a Comment