Debian Codegraph @

src/doc/manual/extensions/packages.txi

raw
1 @node Package local nicknames
2 @section Local package nicknames
3
4 @cindex Package local nicknames
5 @ftindex PACKAGE-LOCAL-NICKNAMES
6
7 @c @menu
8 @c * Package local nicknames overview::
9 @c * Package local nicknames dictionary::
10 @c @end menu
11
12 @subsection Overview
13
14 ECL allows giving packages local nicknames: they allow short and
15 easy-to-use names to be used without fear of name conflict associated
16 with normal nicknames.
17
18 A local nickname is valid only when inside the package for which it
19 has been specified. Different packages can use same local nickname for
20 different global names, or different local nickname for same global
21 name.
22
23 The keyword @code{:package-local-nicknames} in @code{*features*}
24 indicates the support for this feature.
25
26 @subsection Package local nicknames dictionary
27
28 @defmac cl:defpackage name [[options]]*
29 Options are extended to include
30
31 @example
32 :local-nicknames (local-nickname actual-package-name)*
33 @end example
34
35 The package has the specified local nicknames for the
36 corresponding actual packages.
37
38 @exindex defpackage and package local nicknames
39 Example:
40
41 @lisp
42 (defpackage :bar (:intern "X"))
43 (defpackage :foo (:intern "X"))
44 (defpackage :quux (:use :cl) (:local-nicknames (:bar :foo) (:foo :bar)))
45 (find-symbol "X" :foo) ; => FOO::X
46 (find-symbol "X" :bar) ; => BAR::X
47 (let ((*package* (find-package :quux)))
48 (find-symbol "X" :foo)) ; => BAR::X
49 (let ((*package* (find-package :quux)))
50 (find-symbol "X" :bar)) ; => FOO::X
51 @end lisp
52 @end defmac
53
54 @lspdef ext:package-local-nicknames
55 @cppdef si_package_local_nicknames
56 @defun {ext:package-local-nicknames} package-designator
57 @end defun
58 @deftypefun cl_object si_package_local_nicknames (cl_object package_designator)
59 Returns an alist of @code{(local-nickname . actual-package)}
60 describing the nicknames local to the designated package.
61
62 When in the designated package, calls to @code{find-package} with any
63 of the local-nicknames will return the corresponding actual-package
64 instead. This also affects all implied calls to @code{find-package},
65 including those performed by the reader.
66
67 When printing a package prefix for a symbol with a package local nickname, the
68 local nickname is used instead of the real name in order to preserve
69 print-read consistency.
70 @end deftypefun
71
72 @lspdef ext:package-locally-nicknamed-by-list
73 @cppdef si_package_locally_nicknamed_by_list
74 @defun {ext:package-locally-nicknamed-by-list} package-designator
75 @end defun
76 @deftypefun cl_object si_package_locally_nicknamed_by_list (cl_object package_designator)
77 Returns a list of packages which have a local nickname for the
78 designated package.
79 @end deftypefun
80
81 @lspdef ext:add-package-local-nickname
82 @cppdef si_add_package_local_nickname
83 @defun {ext:add-package-local-nickname} local-nickname actual-package &optional package-designator
84 @end defun
85 @deftypefun cl_object si_add_package_local_nickname (cl_object local_nickname, cl_object actual_package, cl_object package_designator)
86 Adds @var{local-nickname} for @var{actual-package} in the designated
87 package, defaulting to current package. @var{local-nickname} must be a
88 string designator, and @var{actual-package} must be a package
89 designator.
90
91 Returns the designated package.
92
93 Signals a continuable error if @var{local-nickname} is already a
94 package local nickname for a different package.
95
96 When in the designated package, calls to @code{find-package} with the
97 @var{local-nickname} will return the package the designated
98 @var{actual-package} instead. This also affects all implied calls to
99 @code{find-package}, including those performed by the reader.
100
101 When printing a package prefix for a symbol with a package local
102 nickname, the local nickname is used instead of the real name in order
103 to preserve print-read consistency.
104 @end deftypefun
105
106 @lspdef ext:remove-package-local-nickname
107 @cppdef si_remove_package_local_nickname
108 @defun {ext:remove-package-local-nickname} old-nickname &optional package-designator
109 @end defun
110 @deftypefun cl_object si_remove_package_local_nickname (cl_object old_nickname, cl_object package_designator)
111 If the designated package had @var{old-nickname} as a local nickname
112 for another package, it is removed. Returns true if the nickname
113 existed and was removed, and @code{nil} otherwise.
114 @end deftypefun
115
116 @node Package locks
117 @section Package locks
118
119 @cindex Package locks
120 @ftindex PACKAGE-LOCKS
121
122 @subsection Package Locking Overview
123
124 ECL borrows parts of the protocol and documentation from SBCL for
125 compatibility. Interface is the same except that the home package for
126 locking is @code{ext} and that ECL doesn't implement Implementation
127 Packages and a few constructs. To load the extension you need to
128 require @code{package-locks}:
129
130 @lisp
131 (require '#:package-locks)
132 @end lisp
133
134 Package locks protect against unintentional modifications of a package:
135 they provide similar protection to user packages as is mandated to
136 @code{common-lisp} package by the ANSI specification. They are not, and
137 should not be used as, a security measure.
138
139 Newly created packages are by default unlocked (see the @code{:lock}
140 option to @code{defpackage}).
141
142 The package @code{common-lisp} and ECL internal implementation packages
143 are locked by default, including @code{ext}.
144
145 It may be beneficial to lock @code{common-lisp-user} as well, to
146 ensure that various libraries don't pollute it without asking,
147 but this is not currently done by default.
148
149 @subsection Operations Violating Package Locks
150
151 The following actions cause a package lock violation if the package
152 operated on is locked, and @code{*package*} is not an implementation
153 package of that package, and the action would cause a change in the
154 state of the package (so e.g. exporting already external symbols is
155 never a violation). Package lock violations caused by these operations
156 signal errors of type @code{package-error}.
157
158 @enumerate
159 @item
160 Shadowing a symbol in a package.
161
162 @item
163 Importing a symbol to a package.
164
165 @item
166 Uninterning a symbol from a package.
167
168 @item
169 Exporting a symbol from a package.
170
171 @item
172 Unexporting a symbol from a package.
173
174 @item
175 Changing the packages used by a package.
176
177 @item
178 Renaming a package.
179
180 @item
181 Deleting a package.
182
183 @item
184 Attempting to redefine a function in a locked package.
185
186 @item
187 Adding a new package local nickname to a package.
188
189 @item
190 Removing an existing package local nickname to a package.
191
192 @end enumerate
193
194 @subsection Package Lock Dictionary
195
196 @lspdef ext:package-locked-p
197 @defun ext:package-locked-p package
198 Returns @code{t} when @var{package} is locked, @code{nil}
199 otherwise. Signals an error if @var{package} doesn't designate a valid
200 package.
201 @end defun
202
203 @lspdef ext:lock-package
204 @defun ext:lock-package package
205 Locks @var{package} and returns @code{t}. Has no effect if package was
206 already locked. Signals an error if package is not a valid
207 @var{package} designator
208 @end defun
209
210 @lspdef ext:unlock-package
211 @defun ext:unlock-package package
212 Unlocks @var{package} and returns @code{t}. Has no effect if
213 @var{package} was already unlocked. Signals an error if @var{package}
214 is not a valid package designator.
215 @end defun
216
217 @lspdef ext:without-package-locks
218 @defmac ext:without-package-locks &body body
219 Ignores all runtime package lock violations during the execution of
220 body. Body can begin with declarations.
221 @end defmac
222
223 @lspdef ext:with-unlocked-packages
224 @defmac ext:with-unlocked-packages (&rest packages) &body body
225 Unlocks @var{packages} for the dynamic scope of the
226 @var{body}. Signals an error if any of @var{packages} is not a valid
227 package designator.
228 @end defmac
229
230 @defmac cl:defpackage name [[option]]* @result{} package
231
232 Options are extended to include
233
234 @example
235 :lock boolean
236 @end example
237
238 If the argument to @code{:lock} is @code{t}, the package is initially
239 locked. If @code{:lock} is not provided it defaults to @code{nil}.
240
241 @exindex Defpackage @code{:lock} option
242 Example:
243
244 @lisp
245 (defpackage "FOO" (:export "BAR") (:lock t))
246
247 ;;; is equivalent to
248
249 (defpackage "FOO") (:export "BAR"))
250 (lock-package "FOO")
251 @end lisp
252 @end defmac