Fork me on GitHub
#clojure
<
2020-12-12
>
Lars Nilsson19:12:20

(defn my-dispatch [x] (...))
(defmulti my-multi #'my-dispatch)

Lars Nilsson19:12:08

Is this the most convenient/best way to be able to work with the defmulti dispatch function in a repl?

didibus20:12:04

There's another trick, hum... I think it's just: (def my-multi nil) (defmulti my-multi ...)

Lars Nilsson20:12:47

Technically, I'm evaluating a clj file in vscode, so I'm not directly typing in the commands in the repl to redefine the dispatch. If it makes a difference..

Lars Nilsson20:12:43

What I'm typing in the repl is mostly the execution of various functions defined. Are you suggesting (def my-multi nil) typed in the repl before saving my file, causing it to be reevaluated would get a new dispatch function defined? (nothing stopping me from trying it out...)

didibus20:12:27

Ya, just put put the (def my-multi nil) above the defmulti definition

didibus20:12:32

Then re-evaluate the file

Lars Nilsson20:12:53

Cool, I'll have to try it out. Thanks a bunch!

didibus20:12:56

Now you can freely change your dispatch function, and it'll be reloaded by defmulti

didibus20:12:53

It's because defmulti has a check that says don't reload if the var is already a multi-method. So it you make the var nil before reloading the check will be false and it'll reload defmulti

didibus20:12:05

There's a difference between this trick and the one you posted before though. In this case you also need to reload the defmethods, cause reloading the defmulti will reset it too. Where as if you pass a var to the dispatch, that's not the case. So it depends what you want to do.

Lars Nilsson20:12:46

My use case at the moment is a fairly small file containing all related functions, so your method would work fine.

👍 3
coby20:12:18

funny, I just came here to ask about the exact same problem 😄

Lars Nilsson19:12:24

(without having to restart the repl..)