I have a data class that looks like this:
data class MyDataCass(val x: String, val y: String,
val something: Something = Something(),
val somethingElse : SomethingElse = SomethingElse())
Since this class is going to be used from both Kotlin and Java I've also created a builder to make it easier to construct it from Java. The problem is that something
and somethingElse
are optional and I'd like to avoid duplication of the default values. This is essentially what I've tried:
data class MyDataCass(val x: String, val y: String,
val something: Something = Something(),
val somethingElse : SomethingElse = SomethingElse()) {
class Builder() {
private lateinit var x: String
private lateinit var y: String
private var something: Something? = null
private var somethingElse: SomethingElse? = null
// builder functions to set the properties defined above
...
fun build() = MyDataCass(x, y, something = ??, somethingElse = ??)
}
}
What I'd like to do is to set something
and somethingElse
only if they've been defined by the builder (i.e. they are not null
), otherwise I'd like to use the default values as defined by the data class constructor. I could of course change the build function to look like this:
fun build() = if (something == null && somethingElse == null) {
MyDataCass(x, y)
} else if(somethingElse == null) {
MyDataCass(x, y, something = something)
} else {
MyDataClass(x,y, somethingElse = somethingElse)
}
}
but I find this to be quite verbose and error prone. Is there a less verbose or more idiomatic way of achieving this?