My curiosity got the better of me:
(require '[clojure.spec.alpha :as spec]
         '[clojure.test :refer [is with-test]])

(spec/fdef local-bindings
  :args (spec/cat :closure fn?)
  :ret (spec/map-of simple-keyword? any?))

(with-test
  (defn local-bindings [closure]
    (into {} 
          (map (fn [^java.lang.reflect.Field field]
                 [(keyword (.getName field))
                  (do
                    (.setAccessible field true)  ; The closed-over locals 
are not public.
                    (.get field closure))]))
          (.getDeclaredFields (class closure))))
  (is (= {:x 3}      (local-bindings (constantly 3))))
  (is (= {}          (local-bindings constantly)))
  (is (= {:f first
          :g second} (local-bindings (juxt first second)))))

I would *not* rely on this code, though. It definitely feels like a hack.
On Thursday, May 24, 2018 at 2:22:42 PM UTC-4, Sonny To wrote:
>
> (defn foo[]
>  (let [x  1]
>    (fn []  
>      (+ x 1)
>     )
>  )
>
> (def bar  (foo))
>
> Is there a way to get the value of x from closure bar?
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to