8 ms·
data class Animal(dog: String, numberOfLegs: Int) and Animal dog = Animal.builder() .setName("dog") .setNumberOfLegs(4) .build(); are very
by Naac 9y ago
data class Animal(dog: String, numberOfLegs: Int)
and
Animal dog = Animal.builder()
.setName("dog")
.setNumberOfLegs(4)
.build();
are very different. In this case the builder pattern is more powerful as it lets you pass around builders that haven't been "built" yet. Kind of like currying.
- dangets 9y agoTrue, but the constructor method is checked/validated at compile time vs. run time with the builder pattern. Not quite the same as your example, but Kotlin does have good syntax for copying data classes while only changing a single field - this could be used similar to your currying usage. https://kotlinlang.org/docs/reference/data-classes.html#copying https://kotlinlang.org/docs/reference/data-classes.html#copy...
- guelo 9y agoHere's that kotlin copy syntax val cat = dog.copy(name = "cat")
- deleted 9y ago[deleted]