Clojure functions, let & return values

Is it unreasonable to return the var variable using let?

(let [pipeline (Channels/pipeline)] (.addLast pipeline "codec" (HttpClientCodec.)) ;; several more lines like this pipeline) 

Is the binding here only a lexical area (as opposed to def) and unsafe to pass through?

Update When writing this question, I realized that it was ugly. And if something is ugly in Clojure, you are probably mistaken.

I think this is probably a more idiomatic way of handling the above (which makes the question controversial, and even convenient knowledge).

 (doto (Channels/pipeline) (.addLast "codec" (HttpClientCodec.))) 
+4
source share
1 answer

let has a purely lexical scope and does not create var. Locals created by let (or loop ) behave exactly like function arguments. So yes, it’s safe to use as many let / loop localized locales as possible, close them, etc. Returning local from a function simply returns its value, not the internal representation (which is actually on the stack if not closed). Thus, let / loop bindings are also fallback / thread safe.

By the way, for your specific code example with a lot of java calls, you might want to use doto instead or in addition. http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/doto

+8
source

Source: https://habr.com/ru/post/1345224/


All Articles