szymanowski
szymanowski

Reputation: 1379

best practice for record method in clojure

i have a record:

(defrecord Foo [a b])

and an instance method for it

(defn inc-a-field [this] (into this {:a (inc (:a this))}))

is it best practice to define a protocol for that? (since it is Foo specific)

Upvotes: 1

Views: 458

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51500

Yes, it's best to define protocol with all desired methods first if you want to attach them to you record type. An alternative is to use ordinary functions with no attachment to your record.

Protocols are very handy for stateful operations. For example, look at carmine connection record implementation.

But if your record is just a map with predefined structure, then it may be better to use ordinary clojure functions instead.

You should also look at this question, it's very similar to yours.

Upvotes: 4

Related Questions