David Kubecka
06/28/2024, 10:41 AMdata class X(a: Int, b: Int, c: Int)
val xBuilder = X.builder(a = 1, b = 2) // type X.Builder ?
xBuilder.build(c=3) // type X
AFAIK there's no nice way how to implement the described use case without code duplication if you don't have control over the data class.hho
06/28/2024, 11:34 AMval builder = X(23,42,999)
val x1 = builder.copy(c = 1)
val x2 = builder.copy(c = 2)
val x3 = builder.copy(c = 3)
David Kubecka
06/28/2024, 12:46 PMephemient
06/28/2024, 1:09 PMDavid Kubecka
06/28/2024, 2:47 PMrocketraman
07/02/2024, 1:58 PMbartek
07/05/2024, 2:01 PMDavid Kubecka
07/08/2024, 10:45 AMephemient
07/08/2024, 1:24 PMDavid Kubecka
07/08/2024, 1:48 PMrocketraman
07/08/2024, 2:40 PMephemient
07/08/2024, 3:42 PMdata class
is the wrong tool anyway https://jakewharton.com/public-api-challenges-in-kotlin/David Kubecka
07/08/2024, 6:09 PMrocketraman
07/08/2024, 6:45 PMrocketraman
07/08/2024, 6:46 PMephemient
07/08/2024, 7:01 PMbartek
07/08/2024, 7:14 PMDavid Kubecka
07/08/2024, 7:16 PMGenerate OpenAPI from code-first annotationsYeah, the spec is not under my control, unfortunately. Still, can I generate a flat API model from a nested data class? (I've not read the docs about this generator part yet)
David Kubecka
07/08/2024, 7:17 PMThe fundamental problem with java-style builder in kotlinYeah, if you write your own then surely there will be problems. That's why I'm asking whether it wouldn't be a good idea to incorporate the feature into the language. I mean, if we already have
copy
why not builders?ephemient
07/08/2024, 7:18 PMX.builder().build()
blowing up at runtimeDavid Kubecka
07/08/2024, 7:20 PMbartek
07/08/2024, 7:28 PMYeah, if you write your own then surely there will be problems. That’s why I’m asking whether it wouldn’t be a good idea to incorporate the feature into the language. I mean, if we already haveIt’s a problem with the pattern, not with building the pattern outside of the default language features. The problem is this:why not builders?copy
data class Shape(val width:Int, val height:Int)
val builder = Shape.builder(width = 100)
builder.build() // nothing prevents you from calling this even though you did not provide `height`
bartek
07/08/2024, 7:29 PMbuild
can produce a valid instance in runtime.bartek
07/08/2024, 7:30 PMShape
☝🏻 you could create smarter builder class tree in which you don’t have build
function unless you set both arguments, but it doesn’t scale past a couple of arguments. Simply not worth it.ephemient
07/08/2024, 7:33 PMdata class Shape(val width: Int, val height: Int) {
class Builder {
fun width(width: Int) = BuilderWithWidth(width)
fun height(height: Int) = BuilderWithHeight(height)
}
class BuilderWithWidth(val width: Int) {
fun width(width: Int) = BuilderWithWidth(width)
fun height(height: Int) = BuilderWithWidthAndHeight(width, height)
}
class BuilderWithHeight(val height: Int) {
fun width(width: Int) = BuilderWithWidthAndHeight(width, height)
fun height(height: Int) = BuilderWithHeight(height)
}
class BuilderWithWidthAndHeight(val width: Int, val height: Int) {
fun width(width: Int) = BuilderWithWidthAndHeight(width, height)
fun height(height: Int) = BuilderWithWidthAndHeight(width, height)
fun build() = Shape(width, height)
}
}
"safe" builders are combinatorial explosion and doesn't scale whether it's written by hand or generatedDavid Kubecka
07/08/2024, 7:36 PM