gpt4 book ai didi

package - 使用包阴影符号

转载 作者:太空宇宙 更新时间:2023-11-03 18:41:41 25 4
gpt4 key购买 nike

例如,我有这个包定义,它隐藏了 COMMON-LISP:LISTEN :

(defpackage :shadows
(:use :common-lisp)
(:shadow #:listen)
(:export #:listen))

然后我想从另一个包中使用这个包,比如说

(defpackage :light
(:use :common-lisp
:shadows))

如果我在使用包时实际上无法覆盖 Common Lisp 符号,那么 shadow 的目的是什么?

最佳答案

简单

defpackage:shadow 参数影响包 shadows 的定义,而不是后来在 light 中使用 shadows

您需要使用 shadowing-import :

(defpackage #:shadows
(:use #:common-lisp)
(:shadow #:listen)
(:export #:listen))
(defpackage #:light
(:shadowing-import-from #:shadows #:listen)
(:use #:common-lisp #:shadows))
(eq 'light::listen 'shadows:listen)
==> T
(describe 'light::listen)
==>
SHADOWS:LISTEN is the symbol SHADOWS:LISTEN, lies in #<PACKAGE SHADOWS>,
is accessible in 2 packages LIGHT, SHADOWS.

#<PACKAGE SHADOWS> is the package named SHADOWS.
It imports the external symbols of 1 package COMMON-LISP and
exports 1 symbol to 1 package LIGHT.

散装

如果您需要进行批量投影,则需要使用单独的函数( make-package , import , export , shadow , use-package ):

(defparameter *my-shadow-list* '(#:car #:cdr))
(make-package '#:my-package :use nil)
(import *my-shadow-list* '#:my-package)
(export *my-shadow-list* '#:my-package)
(shadow *my-shadow-list* '#:my-package)
(use-package '#:cl '#:my-package)
(make-package '#:my-user)
(shadowing-import *my-shadow-list* '#:my-user)
(use-package '(#:cl #:my-package) '#:my-user)
(describe 'my-user::car)
==>
MY-PACKAGE:CAR is the symbol MY-PACKAGE:CAR, lies in #<PACKAGE MY-PACKAGE>,
is accessible in 2 packages MY-PACKAGE, MY-USER.

#<PACKAGE MY-PACKAGE> is the package named MY-PACKAGE.
It imports the external symbols of 1 package COMMON-LISP and
exports 2 symbols to 1 package MY-USER.

您可能会找到 macroexpand-1有助于决定如何到达你需要去的地方:

(macroexpand-1 '(defpackage #:light
(:shadowing-import-from #:shadows #:listen)
(:use #:common-lisp #:shadows)))
==>
(EVAL-WHEN (:COMPILE-TOPLEVEL :LOAD-TOPLEVEL :EXECUTE)
(SYSTEM::%IN-PACKAGE "LIGHT" :NICKNAMES 'NIL :USE 'NIL :CASE-SENSITIVE NIL
:CASE-INVERTED NIL)
(SYSTEM::SHADOWING-IMPORT-CERROR "LISTEN" "SHADOWS" NIL "LIGHT")
(USE-PACKAGE '("COMMON-LISP" "SHADOWS") "LIGHT") (FIND-PACKAGE "LIGHT")) ;
T

附言。隐藏 ANSI CL 标准名称对于代码的可读性来说不是一个好主意。

关于package - 使用包阴影符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44596132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com