https://kotlinlang.org logo
#announcements
Title
# announcements
h

hallvard

10/30/2019, 3:07 PM
Does a combination of
data 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?
s

streetsofboston

10/30/2019, 3:08 PM
You really don’t need a builder pattern if you use default argument values on the data-class’ constructor
👆 2
h

hallvard

10/30/2019, 3:13 PM
I hardly believe default values make the setters return
this
. Whether I need a builder pattern or not is a different question.
d

Dias

10/30/2019, 3:16 PM
what's the use case?
m

Mike

10/30/2019, 3:17 PM
If you use a data class, and default values, and named parameters, and you're only coding in Kotlin, then you shouldn't need/want the builder pattern. If you want a function to set and return this, you'll have to code it yourself, but
apply
helps with that.
1
c

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 callback
s

streetsofboston

10/30/2019, 3:20 PM
Copy code
data 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.
h

hallvard

10/30/2019, 3:23 PM
Yes. I'd like one in a K/JS project where I expose certain data class like objects to users, and instead of letting them insert 6 or 10 or 15 different properties in one long constructor, I like to present them with a builder.
So the question is actually the other way around: do I need a
data 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.
s

streetsofboston

10/30/2019, 3:24 PM
That (avoiding the need to provide too many arguments) you can accomplish with default argument values, requiring the users of your class to only provide the values that are different/non-default.
h

hallvard

10/30/2019, 3:25 PM
No. Sometimes, I cannot give defaults.
s

streetsofboston

10/30/2019, 3:26 PM
? Then how does you builder pattern work, where the user of your class has to provide the value of all arguments/setters?
h

hallvard

10/30/2019, 3:26 PM
Besides, you have answered my question long ago: a combination of
data class
and builder pattern does not exist out of the box.
s

streetsofboston

10/30/2019, 3:27 PM
If you are worried about readability, the users of your class can use the named-argument way of providing values to the constructor, to make the call to the constructor more clear. Note; if your (data) class is not immutable, just use
MyDataClass().apply { ..... }
and set the mutable properties in the `apply`’s lambda
h

hallvard

10/30/2019, 3:31 PM
Yeah, well, I won't use data classes, and that really solves my case. Thanks again.
c

Casey Brooks

10/30/2019, 3:34 PM
Yeah, nothing exists out of the box for this. But you might also consider creating a DSL for configuring these objects instead of the traditional Java-style Builder pattern https://kotlinlang.org/docs/reference/type-safe-builders.html
h

hallvard

10/30/2019, 3:45 PM
JS
3 Views