13 ms·
Not that nice compared to kotlin: data class Animal(dog: String, numberOfLegs: Int) val dog = Animal( name = "dog", numberOfLegs = 4
by guelo 9y ago
Not that nice compared to kotlin:
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.
- jimmaswell 9y agoVery similar in C#: var dog = new Animal(){ name="dog", numberOfLegs=4 }; I use it all the time.
- bunderbunder 9y agoThe big difference there would be, in the C# example you've still got to write a bunch more boilerplate for Equals(), GetHashCode() and ToString(). Kotlin's data classes give you those three for free.
- emidln 9y agoIn clojure I'd write this, if I actually needed a class for type dispatch or interface implementation for some reason. (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.
- billrobertson42 9y agoI mucked around with records a lot early on while learning Clojure, and I'm not really sure when I stopped and just started using maps. Now it's second nature and I'd only consider using a record if there was some overriding performance concern.
- Naac 9y agodata 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]
- andmed3 9y agoAgreed. The amount of boilerplate is reduced significantly in Kotlin. it has builder methods also which help greatly with data classes like apply(), with() and let() for us it became language of choice for backend for these reasons
- NovaX 9y agoNope, we just add @AutoBuilder https://github.com/h908714124/auto-builder https://github.com/h908714124/auto-builder
- guelo 9y agoA code generator for your code generator.
- escherize 9y agonow in... clojure? We usually don't use dynamic dispatch, but if you wanted to explicitly have an animal-ey map with a name and number of legs, it's: (def dog #:animal{:name "dog" :number-of-legs 4})