Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created April 4, 2017 09:04
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cgrand/be2fcab9c3c2cd8e0d0a1f490696a05b to your computer and use it in GitHub Desktop.
Did you know? #clojure
;; did you know that binding conveying is not immutable but read-only? (the future sees updates performed by the original thread)
=> (with-local-vars [a 1]
(future (Thread/sleep 1000) (prn 'future @a))
(var-set a 2))
2
future 2
;; you have to push new bindings to isolate:
=> (with-local-vars [a 1]
(with-bindings {a @a}
(future (Thread/sleep 1000) (prn 'future @a)))
(var-set a 2))
2
future 1
;; but it's a var-by-var isolation:
=> (with-local-vars [a 1
b 'one]
(with-bindings {a @a}
(future (Thread/sleep 1000) (prn 'future @b)))
(var-set b 'two))
two
future two
;; to completely isolate from upstream changes, one has to do:
=> (with-local-vars [a 1
b 'one]
(with-bindings (get-thread-bindings)
(future (Thread/sleep 1000) (prn 'future @b)))
(var-set b 'two))
two
future one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment