>
>
> (def Recipient
>> (s/either PlaceHolder
>> Existing
>> OneOff))
>>
>
> This looks interesting. Where would I actually use this? I mean, if I have
> created three records, I may as well implement multi methods or protocols,
> right? Even if I don't do those, I will still need to use `(condp instance?
> obj ...)` or equivalent to select the appropriate branch for processing. Is
> there a way I can use Recipient to select a branch?
>
I probably wouldn't use protocols since I doubt there is a function
signature that is exactly identical for all branches. Each branch probably
needs access to different parts of your system (eg. database) and always
passing everything to everything is not ideal.
Multi-Method is great if you want something openly extensible but that
again seems unlikely here and also assumes that everything requires the
same arguments.
cond(p) sounds perfect for this case. I tend to write each branch as a
single function and keep the dispatch as compact as possible.
(defn send-placeholder [thing {:keys [text]}]
...)
(defn send-existing [db thing {:keys [contact-id]}]
...)
(defn send-one-off [something thing {:keys [name email]}]
...)
(defn send [app thing recipient]
(condp instance? recipient
PlaceHolder
(send-placeholder thing recipient)
Existing
(send-existing (:db app) thing recipient)
OneOff
(send-one-off (:something app) thing recipient)))
That greatly reduces the cognitive load when looking at each separate
implementation and also keeps the actual internal structure of the
Recipient out of the dispatch. The conpd does not need to know how many
fields are in OneOff, the tuple/vector/variant match versions must know
that.
/thomas
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.