Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

lisp - Is that possible I lexical binding special var in other package?

I have a very weird requirement that lexically binding a special variable of another package.

In file A.lisp

(defpackage A
  (:use #:CL)
  (:export #:a-test))
(in-package A)
(declaim (special *a-sp-v*))
(defun a-test (&key (v *a-sp-v*))
  (print v))

in file B.lisp

(defpackage B
  (:use #:CL #:A))
(in-package B)

(defun b-test ()
  (let ((*a-sp-v* 1))  ; cannot let a-test know
    (a-test)))

I keep receiving the error The variable A::*A-SP-V* is unbound. in my REPL.

Are there some methods I can bind *a-sp-v* in the package B? Let a-test function running with special *a-sp-v* no matter what package it is in?

question from:https://stackoverflow.com/questions/65948149/is-that-possible-i-lexical-binding-special-var-in-other-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is fine (I would use defvar instead of declaim special though). You can bind any symbol, it does not matter which package it resides in.

You just need to make sure you bind the symbol you need to bind and not something else. E.g., since you are not exporting a::*a-sp-v* from a, your b-test binds b::*a-sp-v* rather than a::*a-sp-v*. If you replace *a-sp-v* with a::*a-sp-v* in b-test, it should work. Alternatively, you can add #:*a-sp-v* to the :export section in (defpackage A ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...