On Sun, May 29, 2011 at 8:56 PM, James Reeves <jree...@weavejester.com> wrote:
> The documentation on records seems to indicate that they be used in
> place of structs, but is there any advantage to using a record if no
> protocols are assigned to it?

Access to a record's in-definition members should be faster than
access to arbitrary keys in a map, but that's it if no protocols are
used.

> For example, I've defined a TcpServer record as part of a library I'm
> developing:
>
>  (defrecord TcpServer
>    [port
>     host
>     backlog
>     handler
>     socket
>     connections])
>
>  (defn tcp-server [& {:as options}]
>    {:pre [(:port options) (:handler options)]}
>    (TcpServer.
>     (:port options)
>     (:host options "127.0.0.1")
>     (:backlog options 50)
>     (:handler options)
>     (atom nil)
>     (atom #{})))
>
> Is this idiomatic Clojure, or should I just use a map instead:
>
>  (defn tcp-server [& {:as options}]
>    (merge
>     {:host "127.0.0.1"
>      :backlog 50
>      :socket (atom nil)
>      :connections (atom #{})
>     options))

I don't think either is non-idiomatic, but I'd probably just use the
map. It's shorter and simpler code, more widely interoperable with
other Clojure facilities, and the member access speedup using a record
is unlikely to matter much in code that is blocked on I/O nearly all
of the time anyway. If you were using it in CPU-intensive work, e.g. a
complex number datatype in heavy number crunching, then the speedup
would matter (although in that specific instance a 1.3
primitive-double vector of length 2 would probably beat a record for
speed unless records or deftypes can have unboxed primitive fields and
you can access these without boxing in 1.3).

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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

Reply via email to