Search code examples
clojureclojurescript

Updating an atom with update and inc fails


I have an atom (in ClojureScript):

(def player
  (atom {:episode 0 ...}))

And I want to increment :episode by one:

(swap! player update :episode inc)

This works perfectly fine in Lein REPL.

However, when run, it throws:

mfp.js:5808 Uncaught Error: No protocol method IDeref.-deref defined for type cljs.core/PersistentHashMap: ...

    at Object.cljs$core$missing_protocol [as missing_protocol] (mfp.js:5808)
    at Object.cljs$core$_deref [as _deref] (mfp.js:7268)
    at cljs$core$deref (mfp.js:9396)
    at mfp$update (mfp.js:36808)
    at mfp.js:18388
    at Function.cljs.core.swap_BANG_.cljs$core$IFn$_invoke$arity$4 (mfp.js:18389)
    at cljs$core$swap_BANG_ (mfp.js:18341)
    at mfp$next_episode (mfp.js:36946)
    at HTMLAnchorElement.<anonymous> (mfp.js:36920)

I can workaround this by using a longer form:

(swap! player assoc :episode (+ 1 (@player :episode)))

However, I would like to know why the first form does not work. Thank you.


Solution

  • As @ez121sl pointed out in the comments, function update is redefined somewhere else in the code, which caused an error.