Hi Arash and Tassilo,
>>>>> Arash Esbati <[email protected]> writes:
>>> ,----[ C-h f symbol-value RET ]
>>> | symbol-value is a built-in function in ‘C source code’.
>>> |
>>> | (symbol-value SYMBOL)
>>> |
>>> | Return SYMBOL’s value. Error if that is void.
>>> | Note that if ‘lexical-binding’ is in effect, this returns the
>>> | global value outside of any lexical scope.
>>> `----
>>
>> Hm, I don't really understand that sentence.
> I had a specific picture in my mind where that last sentence might
> apply, but after taking your examples and playing with them, I see that
> I was wrong. Hence, I admit I don't understand that last sentence
> either.
My understanding of the last sentence is as follows:
----------------------------------------------------------------------
(setq foo 1) ; dynamic binding without defvar.
(defun bar (var)
(symbol-value var))
(defun xyz ()
(let ((foo foo))
(setq foo (1+ foo))
(bar 'foo)))
(xyz) ; => 1
foo ; => 1
(special-variable-p 'foo) ; => nil
----------------------------------------------------------------------
If I understand correctly, this example shows that
1. `setq' on foo without `defvar' creates dynamic binding, but doesn't
mark it as special.
2. Since foo isn't special, `let' in the function xyz creats lexical
binding, not dynamic binding.
3. `setq' in xyz changes its lexical value, not dynamic value.
4. `symbol-value' in the function bar accesses its dynamically
bound value, so it returns 1, not 2.
5. After the execution of xyz, let binding disappears and the original
dynamic value of 1 appears as the value of foo.
The sentence of
,----
| Note that if ‘lexical-binding’ is in effect, this returns the
| global value outside of any lexical scope.
`----
refers to the item 4 above.
Regards,
Ikumi Keita