hallvard
10/30/2019, 3:07 PMdata class
and builder pattern exist, or is it even possible? I'd like the simplicity of a data class, but with setters that return this
... Anyone?streetsofboston
10/30/2019, 3:08 PMhallvard
10/30/2019, 3:13 PMthis
. Whether I need a builder pattern or not is a different question.Dias
10/30/2019, 3:16 PMMike
10/30/2019, 3:17 PMapply
helps with that.Casey Brooks
10/30/2019, 3:17 PM.apply { }
is the simple way to turn anything into a Builder pattern. Or if the data class is immutable, you’ll need to use .let { }
and copy the object in each callbackstreetsofboston
10/30/2019, 3:20 PMdata class MyDataClass(val param1: Int = 1, val param2: String = "", val param3: Boolean = false)
...
val data = MyDataClass(param2 = "Hello")
val data2 = MyDataClass(
param1 = 2,
param2 = "Two",
param3= true
)
val data3 = data.copy(param3 = true)
I see no need for a builder-pattern with default argument values.
If you need one for some reason, you’d have to roll your own, with a Builder
inner class with mutable properties, building an immutable data-class.hallvard
10/30/2019, 3:23 PMdata class
? It sure would simplify a few things, but it seems I have to code a bit myself whether I do it the one way or the other.streetsofboston
10/30/2019, 3:24 PMhallvard
10/30/2019, 3:25 PMstreetsofboston
10/30/2019, 3:26 PMhallvard
10/30/2019, 3:26 PMdata class
and builder pattern does not exist out of the box.streetsofboston
10/30/2019, 3:27 PMMyDataClass().apply { ..... }
and set the mutable properties in the `apply`’s lambdahallvard
10/30/2019, 3:31 PMCasey Brooks
10/30/2019, 3:34 PMhallvard
10/30/2019, 3:45 PM