Why does Clojure defmulti dispatch function receive 0 args?

前端 未结 1 1559
不知归路
不知归路 2021-01-21 10:54

Begin Edit:

Mea Culpa! I apologize.

I was running this in the cake repl at Clojure 1.2.1, and it honestly did not work. Now it does after exiting cake repl and a

相关标签:
1条回答
  • 2021-01-21 11:22

    The problem could perhaps be related to the undocumented defonce-like behavior of defmulti.

    If you reload a namespace that contains a (defmulti foo ...) form then that defmulti won't be updated. This often means that the dispatch function will not be updated but all method implementations (in the same namespace) will. (defmulti foo ...) does nothing if the foo var is already bound to a value.

    To fix this in a REPL, remove the multimethod var (ns-unmap 'the.ns 'the-multimethod) and then reload the namespace (require 'the.ns :reload).

    To prevent this problem you can define the dispatch function separately and pass its var to defmulti like this:

    (defn foo-dispatch [...]
      ...)
    
    (defmulti foo #'foo-dispatch)
    

    When the code looks like this it's enough to reload the namespace if you make a change to foo-dispatch.

    0 讨论(0)
提交回复
热议问题