Common Lisp:从包中临时导入一些函数的最佳方法

Cla*_*ley 5 lisp macros common-lisp

有没有办法使用标准的common-lisp函数/宏将一些函数从包临时导入到当前包中?

我找不到一个,不得不自己动手.如果标准已经提供了这样的功能,我宁愿不必编写任何代码或引入另一种语言结构.

(defmacro with-functions (functions the-package &body body)
  "Allows functions in the-package to be visible only for body.
  Does this by creating local lexical function bindings that redirect calls
  to functions defined in the-package"
  `(labels
     ,(mapcar (lambda (x) `(,x (&rest args)
                               (apply (find-symbol ,(format nil "~:@(~a~)" x) 
                                                   ,the-package)
                                      args)))
              functions)
     ,@body))
Run Code Online (Sandbox Code Playgroud)

用法示例:

(defclass-default test-class ()
  ((a 5 "doc" )
   (b 4 "doc")))
#<STANDARD-CLASS TEST-CLASS>
CL-USER> 
(with-functions (class-direct-slots slot-definition-name) 'sb-mop
  (with-functions (slot-definition-initform) 'sb-mop
    (slot-definition-initform
      (car (class-direct-slots (find-class 'test-class))))))
5
CL-USER> 
Run Code Online (Sandbox Code Playgroud)

编辑:将一些Rainer的建议纳入宏.

我决定保留运行时查找功能,以运行时查找的时间成本来查找包中的函数.

我试着写一个使用shadowing-import和unintern的with-import宏,但我无法让它工作.我有读者的问题,在导入函数的代码被评估之前,导入的函数尚未存在(在读取时).

我认为让它与shadowing-import和unintern一起工作是一种更好的方法,因为它会更清晰,更快(尽管没有运行时查找功能)并且可以在包中使用函数和符号.

我很想知道是否有人可以使用unintern和shadowing-import编写带有导入宏的代码.

Vse*_*kin 1

您可以使用要导入的合格符号(即或)import列表,然后使用它们。package:symbolpackage::symbolunintern