2013-08-28 103 views

回答

10

var s暫時由with-local-vars創建。現有的var s暫時被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... 
2

(帶-綁定)預計綁定地圖的鍵是瓦爾,不符號。它將var/values的給定映射推送到線程本地綁定堆棧上,並注意在給定函數返回後將其刪除。這是一個低級功能。

(with-local-vars)允許您使用命令式樣式(變異狀態)進行編碼。

相關問題