# How to avoid defmulti being defonce with the REPL session?

**URL:** https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000
**Category:** How to?
**Tags:** macros
**Created:** [March 16, 2019, 4:46pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000 "2019-03-16T16:46:29Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [March 16, 2019, 4:46pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/1 "2019-03-16T16:46:29Z")

</div>

(note: using Cider)  
I use defmulti and defmethod in my code, `eval-buffer`, and test them. They have an error, so I rewrite them (change them to receive a map instead of sequential args). However, `eval-buffer` won’t update the definition; only restarting the repl session seems to capture my changes. Is there a way to cause macros like defmulti to reload without restarting my repl?

---

<div class="post-metadata">

### Author: ![pesterhazy](https://clojureverse.org/user_avatar/clojureverse.org/pesterhazy/32/395_2.png) [@pesterhazy](https://clojureverse.org/u/pesterhazy)
#### Post date: [March 16, 2019, 5:06pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/2 "2019-03-16T17:06:28Z")

</div>

`defmulti` defaults to defonce semantics. Add a `def` before the `defmulti` declaration to get def semantics instead:

```auto
(def foo nil)
(defmulti foo (fn [n m] n))
(defmethod foo :default [n m] (prn [:n n]))

```

---

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [March 16, 2019, 5:27pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/3 "2019-03-16T17:27:30Z")

</div>

Does this work with all macros?

---

<div class="post-metadata">

### Author: ![pesterhazy](https://clojureverse.org/user_avatar/clojureverse.org/pesterhazy/32/395_2.png) [@pesterhazy](https://clojureverse.org/u/pesterhazy)
#### Post date: [March 16, 2019, 6:45pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/4 "2019-03-16T18:45:24Z")

</div>

`defmulti` is indeed a macro but so far as I can see whether or not it’s implemented as a macro is not relevant to its reevaluation behavior.

---

<div class="post-metadata">

### Author: ![plexus](https://clojureverse.org/user_avatar/clojureverse.org/plexus/32/6_2.png) [@plexus](https://clojureverse.org/u/plexus)
#### Post date: [March 16, 2019, 7:16pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/5 "2019-03-16T19:16:28Z")

</div>

Indeed, defmulti does this because otherwise reevaluating the defmulti would invalidate all the corresponding defmethods.

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [March 17, 2019, 10:14pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/6 "2019-03-17T22:14:39Z")

</div>

Oh, interesting. I always thought it was an issue because of class generation. I’m not sure I totally follow the logic, like, what scenario would it be a problem? Since namespaces are loaded once only anyways.

---

<div class="post-metadata">

### Author: ![HolyJak](https://clojureverse.org/user_avatar/clojureverse.org/holyjak/32/1744_2.png) [@HolyJak](https://clojureverse.org/u/HolyJak)
#### Post date: [March 19, 2019, 12:02pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/7 "2019-03-19T12:02:25Z")

</div>

Perhaps [https://github.com/clojure/tools.namespace#reloading-code-motivation](https://github.com/clojure/tools.namespace#reloading-code-motivation) could help? It mentions defmulti so I suppose it works with that.

---

<div class="post-metadata">

### Author: ![orestis](https://clojureverse.org/user_avatar/clojureverse.org/orestis/32/279_2.png) [@orestis](https://clojureverse.org/u/orestis)
#### Post date: [March 19, 2019, 2:46pm UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/8 "2019-03-19T14:46:36Z")

</div>

What about clojurescript where everything is reloaded when a file changes? Any tips on handling this? I vaguely remember seeing something like:

```auto
(def cool-multi nil)
(defmethod cool-multi ,,,)

```

But I’m not sure if that was the right thing to do or not.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [September 18, 2019, 2:46am UTC](https://clojureverse.org/t/how-to-avoid-defmulti-being-defonce-with-the-repl-session/4000/9 "2019-09-18T02:46:38Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
