Very shortly, you can think of setq
, which is the expansion of setf
you have observed, as doing only one half of what defvar
or defparameter
do:
Consider that defparameter
does this:
(declaim (special x))
(setq x 10)
I.e. it both provides some meta-data (the data about what sort of thing x
is) to the compiler (in this case it tells it that it is a "special" variable) and assigns value.
In particular, defvar
will not behave like this, if it is a top-level form. The standard behaviour is to initialize the value-cell of the symbol only once, so its code will be even more complex, something that you can think of as:
(unless (boundp x) ; This is not entirely correct, because if the symbol
; is otherwise known to the environment, but is unbound
; defvar will not re-bind it, but I can't think of a way
; to mimic that behavior
(declaim (special x))
(setq x 10))
The meta-data provided to compiler may or may not have any effect on how the code will behave. In general, the meta-data is supposed to aid the compiler to make better judgement of the intent behind your code, and thus may result in optimizations. But it can also be useful for documentation or debugging.
You can read about special
and declare
in Hyperspec.