clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
Lars Nilsson 2020-12-12T19:49:20.452200Z

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

Lars Nilsson 2020-12-12T19:50:08.452900Z

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

Lars Nilsson 2020-12-12T19:50:24.453100Z

(without having to restart the repl..)

2020-12-12T20:04:04.453200Z

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

Lars Nilsson 2020-12-12T20:05:47.453400Z

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 Nilsson 2020-12-12T20:07:43.453700Z

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...)

2020-12-12T20:11:27.454Z

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

2020-12-12T20:11:32.454200Z

Then re-evaluate the file

Lars Nilsson 2020-12-12T20:11:53.454400Z

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

2020-12-12T20:11:56.454600Z

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

2020-12-12T20:12:53.454800Z

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

2020-12-12T20:14:05.455Z

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 Nilsson 2020-12-12T20:14:46.455200Z

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

1👍
coby 2020-12-12T20:24:18.455500Z

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