Reb*_*bin 8 clojure dynamic-binding
对于Clojure的文档with-local-vars,并with-bindings不足以对我来说,区分这两个.任何提示?
A. *_*ebb 11
新var的临时创建with-local-vars.现有的vars暂时反弹with-bindings.在这两种情况下,绑定都是线程本地的.
请注意,with-bindings就我所知,主要是通过使用返回的映射来传递来自另一个上下文的绑定的帮助器get-thread-bindings.binding不导入绑定时,类似的功能会更典型.
说明性示例:
(binding [*out* (new java.io.StringWriter)]
(print "world!") (str "hello, " *out*))
;=> "hello, world!"
(with-local-vars [*out* (new java.io.StringWriter)]
(print "world!") (str "hello," *out*))
;=> world!"hello,#<Var: --unnamed-->"
(with-local-vars [foo (new java.io.StringWriter)]
(.write @foo "world") (str "hello, " @foo))
;=> "hello, world"
(binding [foo (new java.io.StringWriter)]
(.write @foo "world") (str "hello, " @foo))
;=> CompilerException java.lang.RuntimeException:
; Unable to resolve var: foo in this context...
Run Code Online (Sandbox Code Playgroud)