Plaster
New
List
Login
common-lisp
default
anonymous
2022.11.27 09:19:55
(in-package :symbol-expansion-test) ;;; File-local symbols. ;; A symbol that starts or ends with "%" becomes file-local. (with-symbol-expansion hidden-symbol-expansion :marker "%") (defvar *foo*% 0) (defclass my-class () ((%foo :initarg :foo :accessor foo :initform nil) (%bar :initarg :bar :accessor bar :initform nil))) (defun sum% (instance) (+ (foo instance) (bar instance) *foo*%)) (defun my-class-test% () (let ((*foo*% 3)) (sum% (make-instance 'my-class :foo 1 :bar 2)))) (print (my-class-test%)) ;;;;;;;;; ;;; When loading the file: ;;; ;;; => 6 ;;; ;;; ;;; In the REPL: ;;; ;;; > (my-class-test%) ;;; ;;; => ERROR: The function MY-CLASS-TEST% is undefined. ;;; ;;; ;;; > (slot-value (make-instance 'my-class :foo 1) '%foo) ;;; ;;; => ERROR: When attempting to read the slot's value (slot-value), ;;; the slot %FOO is missing from the object #<MY-CLASS {1005811BB3}>. ;;; It has a slot #:%FOO, while SYMBOL-EXPANSION-TEST::%FOO is requested. ;;; ;;; ;;; > *foo*% ;;; ;;; => ERROR: The variable *FOO*% is unbound. ;;; Cool side effect: ;;; ;;; I wanted to rename the function %SUM to SUM%, but forgot to rename ;;; the call to %SUM in MY-CLASS-TEST%. When recompiling the file the ;;; compiler immediately told me that the function %SUM doesn't ;;; exist. In other words, MY-CLASS-TEST% didn't just happily use the ;;; old definition of %SUM, because it didn't stick around (which ;;; normally would have lead to unintended behaviour and ;;; head-scratching).
Raw
Annotate
Repaste
Edit