[英]let vs def in clojure
I want to make a local instance of a Java Scanner
class in a clojure program.我想在 clojure 程序中创建 Java Scanner
class 的本地实例。 Why does this not work:为什么这不起作用:
; gives me: count not supported on this type: Symbol
(let s (new Scanner "a b c"))
but it will let me create a global instance like this:但它会让我创建一个像这样的全局实例:
(def s (new Scanner "a b c"))
I was under the impression that the only difference was scope, but apparently not.我的印象是,唯一的区别是 scope,但显然不是。 What is the difference between let
and def
? let
和def
和有什么不一样?
The problem is that your use of let
is wrong.问题是您对let
的使用是错误的。
let
works like this: let
像这样工作:
(let [identifier (expr)])
So your example should be something like this:所以你的例子应该是这样的:
(let [s (Scanner. "a b c")]
(exprs))
You can only use the lexical bindings made with let within the scope of let (the opening and closing parens).您只能在 let 的 scope 中使用 let 的词法绑定(左括号和右括号)。 Let just creates a set of lexical bindings.让我们创建一组词法绑定。 I use def for making a global binding and lets for binding something I want only in the scope of the let as it keeps things clean.我使用 def 进行全局绑定,并让 let 仅在 let 的 scope 中绑定我想要的东西,因为它可以保持干净。 They both have their uses.他们都有自己的用途。
NOTE: (Class.) is the same as (new Class), it's just syntactic sugar.注意: (Class.) 与 (new Class) 相同,只是语法糖。
LET is not "make a lexical binding in the current scope", but "make a new lexical scope with the following bindings". LET 不是“在当前范围内进行词法绑定”,而是“使用以下绑定创建新的词法 scope”。
(let [s (foo whatever)] ;; s is bound here ) ;; but not here
(def s (foo whatever)) ;; s is bound here
Simplified: def is for global constants, let is for local variables.简化: def用于全局常量, let用于局部变量。
Correct syntax:正确的语法:
(let [s (Scanner. "a b c")] ...)
You could think of let
as syntactic sugar for creating a new lexical scope with fn
then applying it immediately:您可以将let
视为使用fn
创建新词法 scope 然后立即应用它的语法糖:
(let [a 3 b 7] (* a b)) ; 21
; vs.
((fn [a b] (* a b)) 3 7) ; 21
So you could implement let
with a simple macro and fn
:所以你可以用一个简单的宏和fn
来实现let
:
(defmacro fnlet [bindings & body]
((fn [pairs]
`((fn [~@(map first pairs)] ~@body) ~@(map last pairs)))
(partition 2 bindings)))
(fnlet [a 3 b 7] (* a b)) ; 21
The syntax for them is different, even if the meanings are related.它们的语法是不同的,即使含义是相关的。
let takes a list of bindings (name value pairs) followed by expressions to evaluate in the context of those binding. let 接受一个绑定列表(名称值对),后跟表达式以在这些绑定的上下文中求值。
def just takes one binding, not a list, and adds it to the global context. def 只接受一个绑定,而不是一个列表,并将其添加到全局上下文中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.