Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Records and structs can grow at runtime (insofar as immutable data structures grow).

    user> (defrecord Foo [x])
    user.Foo
    user> (assoc (Foo. 1) :y 2)
    #:user.Foo{:x 1, :y 2}

    user> (defstruct foo :x)
    #'user/foo
    user> (assoc (struct-map foo :x 1) :y 2)
    {:x 1, :y 2}


Well, what do you know, Clojure records and hash-tables are equal (strongly.)

I will announce this great news to a few of my closest friends :-)


Almost. Record types are checked as part of full equality. So record Foo{:x 1} is not equal to Bar{:x 1} or to {:x 1}.


Almost. Record types are checked as part of Clojure's "=", but not as part of Java's ".equals". See [1] and linked material. This means that Foo{:x 1} and Bar{:x 1} will collide in sets and as map keys (of Clojure or Java varieties). This seems a bit icky to me, but seems to be intentional.

[1] http://dev.clojure.org/jira/browse/CLJ-736


Records also provide better performance than hash tables for their defined keys.

You can think of them as a Java class that has certain fields (it's named fields) and also implements the Map interface for additional expandability. Records provide "accessor" methods for their named fields that provide bare-metal JVM performance.




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

Search: