by guelo · 9y ago · 0 comments
data class Animal(dog: String, numberOfLegs: Int)
val dog = Animal(
name = "dog",
numberOfLegs = 4
)
The amount of boilerplate for AutoValue builders is so ridiculous that most people use IDE plugins to generate it. (s/defrecord Animal
[name :- String
number-of-legs :- Integer])
(def dog (map->Animal
{:name "dog"
:number-of-legs 4}))
The nice thing is that I could also skip the defrecord if I just want enforcement of the shape of the data by making a similar defschema and then I'd just use plain maps. 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.https://kotlinlang.org/docs/reference/data-classes.html#copy...
val cat = dog.copy(name = "cat")var dog = new Animal(){ name="dog", numberOfLegs=4 };
I use it all the time.
(def dog #:animal{:name "dog" :number-of-legs 4})