Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defparameter always binds the special variable, so the initial-value
- is required, so NIL is invented from no values.
- On the other hand, in the case of defvar, without an initial-value the
- variable is left alone, unbound if it was unbound, bound to the old
- value if it was bound.
- You could want to map no value to a defvar form without initial-value,
- but this would imply evaluating the function at compilation time,
- instead of run-time when the initial-value expression is normally
- evaluated. Therefore we should define a new macro, similar to defvar,
- but with this amended semantic.
- (defmacro define-variable (name &optional (initial-expression nil initial-expression-p) docstring)
- (if initial-expression-p
- `(progn
- (eval-when (:compile-toplevel)
- (defvar ,name)
- ,@(when docstring
- `((setf (documentation ',name 'variable) ,docstring))))
- (eval-when (:load-toplevel :execute)
- (defvar ,name)
- ,@(when docstring
- `((setf (documentation ',name 'variable) ,docstring)))
- (let ((values (multiple-value-list ,initial-expression)))
- (if values
- (setf ,name (first values))
- (makunbound ',name)))))
- `(defvar ,name)))
- (pprint (macroexpand-1 '(define-variable *foo* (if (zerop (random 2)) (values 1) (values)) "Bound or not, randomly.")))
- (progn (eval-when (:compile-toplevel)
- (defvar *foo*)
- (setf (documentation '*foo* 'variable) #1="Bound or not, randomly."))
- (eval-when (:load-toplevel :execute)
- (defvar *foo*)
- (setf (documentation '*foo* 'variable) #1#)
- (let ((values (multiple-value-list (if (zerop (random 2)) (values 1) (values)))))
- (if values (setf *foo* (first values)) (makunbound '*foo*)))))
- t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement