How to flatten a potentially null object to argume...
# getting-started
d
How to flatten a potentially null object to arguments of a data class constructor (with default values)?
Copy code
data class Coordinates(val x: Int, val y: Int)

// NOTE: I cannot change this as it comes from 3rd party system.
data class CoordinatesAndColor(val color: String, val x: Int = 0, val y: Int = 0)

// is actually passed from outside so it can be null
val coords: Coordinates? = Coordinates(1, 1)

// now I would like to do something akin to this (in "pseudo-code")
CoordinatesAndColor(color = "red").apply {
  // if coords is null the default values should be used
  coords?.let {
    x = it.x
    y = it.y
  }
}

// currently the only way I know is this
CoordinatesAndColor(color = "red", x = coords?.x ?: 0, y = coords?.y ?: 0)
Any ideas?
j
I would probably use composition instead of redefining the
x
and
y
properties, which solves the problem altogether. It would also allow to use methods/extensions of
Coordinates
more easily with
CoordinatesAndColor
instances. You could still define
x
and
y
extension properties that delegate to the underlying coordinates object.
d
Yeah, I should have mentioned that I cannot modify
CoordinatesAndColor
(updating the post)
j
Then I'm afraid that you don't really have a choice. Either you repeat and hardcode the default values for
x
and
y
like in your last example, or you use an
if
statement to call the 1-arg constructor or the 3-arg constructor based on the nullilty of `coords`:
Copy code
// custom factory function
fun CoordsAndColor(color: String, coords: Coordinates?) = if (coords == null) {
    CoordsAndColor(color = color)
} else {
    CoordsAndColor(color = color, x = coords.x, y = coords.y)
}
k
Possibly the generated
copy
function could be used:
Copy code
CoordinatesAndColor(color = "red").let {
    if (coords == null)
        it
    else
        copy(x = it.x, y = it.y)
}
If it's actually more complicated, with more parameters and more conditions, you might want to create a builder.
👍 1
j
Good point, I didn't think of that. It must be noted that we create 2 instances in this case, which might be problematic if the use case is performance sensitive (which might happen with geometry stuff)
k
Indeed, in which case, your solution is better. The real question is, why do we have to repeat the default values like this line in the original question:
Copy code
CoordinatesAndColor(color = "red", x = coords?.x ?: 0, y = coords?.y ?: 0)
It could have been useful if the language allowed something like:
Copy code
CoordinatesAndColor(color = "red", x = coords?.x ?: default, y = coords?.y ?: default)
j
Yep, I think there was a YouTrack issue for this. I have needed this a couple times in the past
k
j
val coords = coordsFromOutside() ?: Coordinates(0,0)
CoordinatesAndColor("red", coords.x, coords.y)
k
This repeats the default values (0). If the default values change, you'll have to change them here too.
1
d
The solution by @Klitos Kyriacou is perfectly acceptable to me (using scope functions, of course). The geometry stuff was just an example; in reality, the code in question is not on the hot path.
j
Ok. Although I personally find it more readable with a simple
if
and 2 different constructor calls depending on the case. Using a combination of nested `let`/`if`/`copy` is a lot of cognitive overhead IMO.
1
d
I think it depends on how many and how complicated are the mandatory arguments. In my case there are a few of them, so the version with
copy
leads to less code duplication.
👍 1