Let's say you have a case class MyConfig(url: String)
.
Now you need to add another property: port: Int
, but without breaking existing serialized values.
You have 2 options:
Option[Int]
, and set a default value later if it is missingInt = MyDefaultValue
to avoid Option
gymnasticsUsing Option[T]
is handy when you don't have a default value.
But it is a bit cumbersome to handle:
case class MyConfig(url: String, port: Option[Int]) derives JsonRW
parsedConfig.port.getOrElse(...)
If you do have a default value for a particular property, by all means do use it.
It will make your life much easier, you can pretend it was always there:
case class MyConfig(url: String, port: Int = 1234) derives JsonRW