groostav
03/06/2017, 7:18 PMcopy
function is effectively a persister (read: local factory) function to make data classes persistent isn't it?
data class Thingy(val x: Double, val y: Int)
val first = Thingy(1.0, 42)
val second = first.copy(x = 2.0)
you dont get any sharing but you do get persistence.elizarov
03/06/2017, 7:32 PMcopy
is great (better than nothing). but what if I want to set x = 2.0
and update y = 3.0
, but only if someProperty
is true? With copy I have to write something like:
val second = first.copy(x = 2.0)
val third = if (someProperty) second else second.copy(y = 3.0)
or
val second = first.copy(x = 2.0, y = if (someProperty) 3.0 else first.y)
The first approach is inefficient, and the second does not scale to conditional update of multiple properties. Neither is plesant nor clear.