user1383359
user1383359

Reputation: 2753

Clojure: ns-resolve -> get an agent -> call send on it

Minimal failure case:

(ns test)

(def a (agent "hello"))

(send a (fn [x] "world")) ; works

(send (ns-resolve 'test 'a) (fn [x] "test")) ; fails

Question:

Why does the last line fail?

This is part of a code hot-loading system. I have to use ns-resolve.

Is there a way to make this work?

Thanks!

Upvotes: 1

Views: 244

Answers (1)

Joost Diepenmaat
Joost Diepenmaat

Reputation: 17773

ns-resolve returns a var, not the value of the var (the agent). you need to deref the var to get the value:

(send (deref (ns-resolve 'test 'a)) (fn [x] "world"))
;; or
(send @(ns-resolve 'test 'a) (fn [x] "world"))

Upvotes: 3

Related Questions