I've seen both (let (var) ...) and (let ((var nil)) ...) is there any difference between these statements?
- 8,786
- 1
- 32
- 114
2 Answers
The documentation for let says:
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
The let (var) variant matches the first line — var is a symbol, bound to nil. The let ((var nil)) variant matches the second line — (var nil) where var is the symbol and the initial value is nil.
They do the same thing in this case.
- 2,541
- 14
- 24
@amitp provided the answer. They do have the same behavior.
However, IMO they can indicate something slightly different to a human reader of the code -- at least according to an informal convention. That is, they can convey a different connotation.
I use (let (foo) ...) only when the initial value is intentionally set in the let body, e.g., in a conditional way. It tells me, as a (later) reader of my own code, that an initial value of nil, which is what it provides, isn't used - makes no difference.
I use let ((foo nil)) ...) to indicate that the nil binding matters -- it really is an intentional initialization. I do it to make the value more obvious.
I do the latter also in the case of binding a global variable for which I know that a nil value has a particular behavior. Using that form, with the explicit nil, points out to me clearly that I'm imposing that nil behavior there. IOW, it just makes the binding more obvious.
- 77,472
- 10
- 114
- 243