Hacker News new | past | comments | ask | show | jobs | submit login

In 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.



I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: