Fork me on GitHub
#mount
<
2018-06-01
>
ben10:06:58

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

ben10:06:48

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

Timo Freiberg10:06:29

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

Timo Freiberg10:06:38

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 🙂

Timo Freiberg10:06:44

you could do this in the REPL:

Timo Freiberg10:06:59

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

ben10:06:19

Thanks, Timo - that's really helpful 👍

Timo Freiberg10:06:38

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

Timo Freiberg10:06:43

glad to help :thumbsup:

ben10:06:06

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

Timo Freiberg10:06:52

not a good phrasing tbh

ben10:06:05

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

ben10:06:13

it's a good phrasing!