On Thu, Sep 26, 2013 at 1:49 AM, Marshall Bockrath-Vandegrift
<llas...@gmail.com> wrote:
> Vincent Chen <noodle...@gmail.com> writes:
>
>> - Use something else than records to model structs (suggestions welcome)?
>
> Maps.
>
> Records have concrete Java types, which allows them to implement
> interfaces and participate in protocols.  Fields defined on a record
> type are backed by JVM object fields, which can increase performance.
> But there are no strictness benefits – a record may have any number of
> additional keys associated to values:
>
>     (defrecord Foo [bar])
>     ;;=> user.Foo
>     (map->Foo {:bar 1, :baz 2})
>     ;;=> #user.Foo{:bar 1, :baz 2}
>     (class (map->Foo {:bar 1, :baz 2}))
>     ;;=> user.Foo
>
> So my suggestion would be to instead turn your `struct` definitions into
> functions validating that the expected fields are present within plain
> maps.  (Assuming some sort of strictness/validation is the goal.)
>
Thanks for the suggestions.

My records (structs) do actually implement protocols, since the
structs represent something which has to be sent over the wire.
Clojure being a dynamic language means that I have to specify the
types of each field of a struct, and I do it as part of a protocol
implemented by the records.

If I'm getting you correctly, you're suggesting that instead of

(defrecord Foo [a b c])

I should rather have

(defn make-foo [a b c]
  {:a a, :b b, :c c})

? That could work, even though I had tried to avoid having to do this
when I started. I guess rather than protocols I'll just use
multimethods instead.

Thanks for your help,

Vincent

-- 
-- 
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/groups/opt_out.

Reply via email to