In today's episode, we talk about multimethods: https://clojuredesign.club/episode/087-polymorphic-metal/
Nice episode. @nate you mentioned about using (def my-interface nil)
to not loose your repl session and keep your defmulti
in sync... there is also another trick that I use to do the same.. I find it a little bit easier (maybe bc I'm used to it)
(defn dispatch-fn [m]
(:nice-logic m))
(defmulti my-interface #'dispatch-fn)
if you re-eval the definition of dispatch-fn
there is no need to re-eval the defmulti, everything is synced! 😃That's a great tip. I'll have to try that next time to prevent wedges related to the dispatch. It's a nice way of controlling the dispatch function outside of the multi. Also, having it as a top level fn makes it easier to print the dispatched return value for debugging.
Exactly! I work processing several files where 70% of their content is the same, but 30% is wildly different. I pray for multimethod everyday! haha. Another tip would be the usage of https://github.com/aleph-io/potemkin import-vars
to combine definitions and implementations in the same namespace. (when you need to have them separate for ny reason)
Let says we have interfaces.clj
and impl1.clj
, impl2.clj
, etc.. now you have several files to remember requiring when you want to use a specific interface+implementation.. you can glue them together in a feature.clj
namespace using import-vars. Now, you are sure that the interface and the implementations are both required and you only need to look into feature.clj
Interesting. I've only heard of potemkin, haven't needed it so far.