On Thu, Oct 16, 2014 at 3:49 PM, James Reeves <ja...@booleanknot.com> wrote:
> > {:name "Alice", :email "al...@example.com"} > > At this point, stop. You have your data model. > > > I think that coming from OO, the most disconcerting piece of Clojure's philosophy is that it is relatively rare in Clojure to publish an API with data accessors like `get-name`. The most common way to access the name would be to simply use (:name person) But let's say later you decide you want your data model to be {:first-name "Alice", :last-name "Beasley", :email "al...@example.com"}, and you want to change name to be a computed value that concatenates first and last names -- this is going to break all your existing code. If name were only accessed throughout your code via the call `get-name`, then it would be trivial to make this change. (Theoretically, it is possible to implement a revision to a person data structure by representing it as a custom map that reimplements keyword access so that (:name person) calls a function rather than does the usual keyword lookup, but I've never seen anyone do this, so I'm ignoring this possibility for the purpose of this discussion). Many OO languages force, or encourage you stylistically, to hide everything behind setters and accessors, so you get for free the ability to change your mind about the data model later. With Clojure, it is very common to allow your data model to *be* the API. This puts some additional pressure to get it right the first time. You have to explicitly think ahead about which fields might need to change in the future. --Mark P.S. To answer your specific questions about what is preferred, I'd go with your version that looks like: (defn make-customer [name email] {:name name, :email email}) (def customer1 (make-customer "Tom" "em...@address.com")) I wouldn't bother with records unless I needed to use protocols. I would use a constructor like make-customer, but I would probably not use an accessor like get-name. I think these would be the most common choices in the Clojure community (although other choices are also valid). -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.