Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 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 Answer

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 ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...