Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uninterned symbols in expanded let-forms of macros #54

Open
TobiasZawada opened this issue Jun 19, 2019 · 3 comments
Open

Use uninterned symbols in expanded let-forms of macros #54

TobiasZawada opened this issue Jun 19, 2019 · 3 comments

Comments

@TobiasZawada
Copy link

Use uninterned symbols in expanded let-forms of macros if the values of these symbols are for macro-internal use only.

;; bad:
(defmacro i-interned (&rest body)
  (declare (debug (body)))
  `(let ((i 1))
     (progn
       (message "Macro internal value of i: %s" i)
       ,@body)))

;; good:
(defmacro i-uninterned (&rest body)
  (declare (debug (body)))
  (let ((i (make-symbol "i")))
    `(let ((,i 1))
       (progn
	 (message "Macro internal value of i: %s" ,i)
	 ,@body))))

(let ((i 0))
  (i-interned
   (message "Value of i: %s" i)))

(let ((i 0))
  (i-uninterned
   (message "Value of i: %s" i)))

Output in the *Messages* buffer:

Macro internal value of i: 1
Value of i: 1
Macro internal value of i: 1
Value of i: 0
@Fuco1
Copy link
Collaborator

Fuco1 commented Jun 19, 2019

Agreed, I've seen this in quite some projects around where things worked just by accident of naming being "compatible".

@rprimus
Copy link

rprimus commented Jun 19, 2019

Wed Jun 19 17:03:21 BST 2019

Question - wouldn't this be a use case for gensym?

@Fuco1
Copy link
Collaborator

Fuco1 commented Jun 19, 2019

You can use gensym or make-symbol, the end result is the same except make-symbol has nicer naming when you macroexpand. gensym is actually a wrapper around make-symbol. But yea, doesn't matter much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants