mount 2018-06-01

Hi all! I attempted to do some refactoring on a project using mount, and I've found that passing a mount.core.DerefableState to a higher order function in order to make a new function results in an error being thrown when that new function is called. I've attempted to display what I mean here 👇

(defstate my-seq :start (load-my-seq ...))

(defn my-function-maker
  [x]
  (fn [f] (map f x)))

;; Doesn't work when called
;; "Don't know how to create ISeq from: mount.core.DerefableState"
(def my-function1 (my-function-maker my-seq))

;; Works okay
(defn my-function2 
  [f]
  (map f my-seq))

Does anyone have any insight into why this is the case? Or where in the documentation I should be looking - I've had no luck so far

the my-seq state starts off uninitialized (as a mount.core.DerefableState). after calling mount/start, the :start function will be called, (load-my-seq ...) in this case, and my-seq will be set to the return value of that function

you don't want compile-time dependencies to the value of runtime states anyway, so what you're doing in my-function1 should hopefully never be necessary 🙂

you could do this in the REPL:

(mount/start)
(def my-function1 (my-function-maker my-seq))

Thanks, Timo - that's really helpful 👍

so the problem is not the higher order function, but that you're calling it in a def

Yep - when you phrase it as compile-time dependencies to the value of runtime states it does sound quite bad, doesn't it?

not a good phrasing tbh

haha no I meant it really highlights why what I was doing is bad

it's a good phrasing!