Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. defparameter always binds the special variable, so the initial-value
  2. is required, so NIL is invented from no values.
  3.  
  4. On the other hand, in the case of defvar, without an initial-value the
  5. variable is left alone, unbound if it was unbound, bound to the old
  6. value if it was bound.
  7.  
  8. You could want to map no value to a defvar form without initial-value,
  9. but this would imply evaluating the function at compilation time,
  10. instead of run-time when the initial-value expression is normally
  11. evaluated. Therefore we should define a new macro, similar to defvar,
  12. but with this amended semantic.
  13.  
  14. (defmacro define-variable (name &optional (initial-expression nil initial-expression-p) docstring)
  15. (if initial-expression-p
  16. `(progn
  17. (eval-when (:compile-toplevel)
  18. (defvar ,name)
  19. ,@(when docstring
  20. `((setf (documentation ',name 'variable) ,docstring))))
  21. (eval-when (:load-toplevel :execute)
  22. (defvar ,name)
  23. ,@(when docstring
  24. `((setf (documentation ',name 'variable) ,docstring)))
  25. (let ((values (multiple-value-list ,initial-expression)))
  26. (if values
  27. (setf ,name (first values))
  28. (makunbound ',name)))))
  29. `(defvar ,name)))
  30.  
  31. (pprint (macroexpand-1 '(define-variable *foo* (if (zerop (random 2)) (values 1) (values)) "Bound or not, randomly.")))
  32.  
  33. (progn (eval-when (:compile-toplevel)
  34. (defvar *foo*)
  35. (setf (documentation '*foo* 'variable) #1="Bound or not, randomly."))
  36. (eval-when (:load-toplevel :execute)
  37. (defvar *foo*)
  38. (setf (documentation '*foo* 'variable) #1#)
  39. (let ((values (multiple-value-list (if (zerop (random 2)) (values 1) (values)))))
  40. (if values (setf *foo* (first values)) (makunbound '*foo*)))))
  41. t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement