1
0
Fork 0
cl-sites/novaspec.org/cl/f_with-package-iterator.html
2025-02-05 18:52:26 +01:00

750 lines
No EOL
16 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>with-package-iterator | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_with-package-iterator.html"
><LINK REL="next" HREF="f_unexport.html" TYPE="text/html" TITLE="unexport"
><LINK REL="prev" HREF="f_make-package.html" TYPE="text/html" TITLE="make-package"
><LINK REL="up" HREF="11_2_Packages_Dictionary.html" TYPE="text/html" TITLE="11.2 Packages Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="11_Packages.html"
>11. Packages</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="11_2_Packages_Dictionary.html"
>11.2 Packages Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_with-package-iterator.html"
>with-package-iterator</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_make-package.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_unexport.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="with-package-iterator"
></SPAN
><B
>with-package-iterator</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Macro</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>with-package-iterator</B
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmssi"
>name</SPAN
> <SPAN CLASS="cmssi"
>package-list-form</SPAN
> <SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>symbol-types</SPAN
><SPAN CLASS="cmr"
>)</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>declaration</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>form</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>result</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
></DIV
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>name</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>package-list-form</VAR
> &#8212; a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>; evaluated once to produce a <VAR CLASS="param"
>package-list</VAR
>. </P
><P CLASS="j"
><VAR CLASS="param"
>package-list</VAR
> &#8212; a <A HREF="26_1_Glossary.html#designator"
><EM CLASS="term"
>designator</EM
></A
> for a list of <A HREF="26_1_Glossary.html#package_designator"
><EM CLASS="term"
>package designators</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>symbol-type</VAR
> &#8212; one of the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:internal</SPAN
></SPAN
>, <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:external</SPAN
></SPAN
>, or <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:inherited</SPAN
></SPAN
>. </P
><P CLASS="j"
><VAR CLASS="param"
>declaration</VAR
> &#8212; a <A HREF="m_declare.html" CLASS="misc"
><B
>declare</B
></A
> <A HREF="26_1_Glossary.html#expression"
><EM CLASS="term"
>expression</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>forms</VAR
> &#8212; an <A HREF="26_1_Glossary.html#implicit_progn"
><EM CLASS="term"
>implicit progn</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>results</VAR
> &#8212; the <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> of the <VAR CLASS="param"
>forms</VAR
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
>Within the lexical scope of the body <VAR CLASS="param"
>forms</VAR
>, the <VAR CLASS="param"
>name</VAR
> is defined via <A HREF="f_flet.html" CLASS="specref"
><B
>macrolet</B
></A
> such that successive invocations of <SPAN CLASS="cmtt"
>(</SPAN
><VAR CLASS="param"
>name</VAR
><SPAN CLASS="cmtt"
>)</SPAN
> will return the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
>, one by one, from the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> in <VAR CLASS="param"
>package-list</VAR
>. </P
><P CLASS="j"
>It is unspecified whether <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> inherited from multiple <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> are returned more than once. The order of <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> returned does not necessarily reflect the order of <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> in <VAR CLASS="param"
>package-list</VAR
>. When <VAR CLASS="param"
>package-list</VAR
> has more than one element, it is unspecified whether duplicate <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> are returned once or more than once. </P
><P CLASS="j"
><VAR CLASS="param"
>Symbol-types</VAR
> controls which <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
> in a <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> are returned as follows:</P
><DL
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:internal</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> in the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>, but that are not <A HREF="26_1_Glossary.html#exported"
><EM CLASS="term"
>exported</EM
></A
>.</P
></DD
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:external</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> in the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> and are <A HREF="26_1_Glossary.html#exported"
><EM CLASS="term"
>exported</EM
></A
>.</P
></DD
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:inherited</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> that are <A HREF="26_1_Glossary.html#exported"
><EM CLASS="term"
>exported</EM
></A
> by used <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> and that are not <A HREF="26_1_Glossary.html#shadow"
><EM CLASS="term"
>shadowed</EM
></A
>.</P
></DD
></DL
><P CLASS="j"
>When more than one argument is supplied for <VAR CLASS="param"
>symbol-types</VAR
>, a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is returned if its <A HREF="26_1_Glossary.html#accessibility"
><EM CLASS="term"
>accessibility</EM
></A
> matches any one of the <VAR CLASS="param"
>symbol-types</VAR
> supplied. Implementations may extend this syntax by recognizing additional symbol accessibility types. </P
><P CLASS="j"
>An invocation of <SPAN CLASS="cmtt"
>(</SPAN
><VAR CLASS="param"
>name</VAR
><SPAN CLASS="cmtt"
>)</SPAN
> returns four values as follows:</P
><OL
><LI
><P CLASS="j"
>A flag that indicates whether a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is returned (true means that a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is returned).</P
></LI
><LI
><P CLASS="j"
>A <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> that is <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
> in one the indicated <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
>.</P
></LI
><LI
><P CLASS="j"
>The accessibility type for that <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>; <I CLASS="i"
><I
>i.e.</I
></I
>, one of the symbols <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:internal</SPAN
></SPAN
>, <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:external</SPAN
></SPAN
>, or <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:inherited</SPAN
></SPAN
>.</P
></LI
><LI
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> from which the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> was obtained. The <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> is one of the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> present or named in <VAR CLASS="param"
>package-list</VAR
>.</P
></LI
></OL
><P CLASS="j"
>After all <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> have been returned by successive invocations of <SPAN CLASS="cmtt"
>(</SPAN
><VAR CLASS="param"
>name</VAR
><SPAN CLASS="cmtt"
>)</SPAN
>, then only one value is returned, namely <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
>The meaning of the second, third, and fourth <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>values</EM
></A
> is that the returned <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
> in the returned <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> in the way indicated by the second return value as follows:</P
><DL
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:internal</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>Means <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> and not <A HREF="26_1_Glossary.html#exported"
><EM CLASS="term"
>exported</EM
></A
>.</P
></DD
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:external</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>Means <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> and <A HREF="26_1_Glossary.html#exported"
><EM CLASS="term"
>exported</EM
></A
>.</P
></DD
><DT
><SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:inherited</SPAN
></SPAN
></DT
><DD
><P CLASS="j"
>Means not <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> (thus not <A HREF="26_1_Glossary.html#shadow"
><EM CLASS="term"
>shadowed</EM
></A
>) but inherited from some used <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>.</P
></DD
></DL
><P CLASS="j"
>It is unspecified what happens if any of the implicit interior state of an iteration is returned outside the dynamic extent of the <A HREF="f_with-package-iterator.html" CLASS="macref"
><B
>with-package-iterator</B
></A
> form such as by returning some <A HREF="26_1_Glossary.html#closure"
><EM CLASS="term"
>closure</EM
></A
> over the invocation <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>. </P
><P CLASS="j"
>Any number of invocations of <A HREF="f_with-package-iterator.html" CLASS="macref"
><B
>with-package-iterator</B
></A
> can be nested, and the body of the innermost one can invoke all of the locally <A HREF="26_1_Glossary.html#establish"
><EM CLASS="term"
>established</EM
></A
> <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macros</EM
></A
>, provided all those <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macros</EM
></A
> have distinct names.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><P CLASS="j"
>The following function should return <SPAN CLASS="misc"
><B
>t</B
></SPAN
> on any <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
>, and signal an error if the usage of <A HREF="f_with-package-iterator.html" CLASS="macref"
><B
>with-package-iterator</B
></A
> does not agree with the corresponding usage of <A HREF="f_do-symbols.html" CLASS="macref"
><B
>do-symbols</B
></A
>.</P
><PRE CLASS="screen"
>(defun test-package-iterator (package)
(unless (packagep package)
(setq package (find-package package)))
(let ((all-entries '())
(generated-entries '()))
(do-symbols (x package)
(multiple-value-bind (symbol accessibility)
(find-symbol (symbol-name x) package)
(push (list symbol accessibility) all-entries)))
(with-package-iterator (generator-fn package
:internal :external :inherited)
(loop
(multiple-value-bind (more? symbol accessibility pkg)
(generator-fn)
(unless more? (return))
(let ((l (multiple-value-list (find-symbol (symbol-name symbol)
package))))
(unless (equal l (list symbol accessibility))
(error "Symbol ~S not found as ~S in package ~A [~S]"
symbol accessibility (package-name package) l))
(push l generated-entries)))))
(unless (and (subsetp all-entries generated-entries :test #'equal)
(subsetp generated-entries all-entries :test #'equal))
(error "Generated entries and Do-Symbols entries don't correspond"))
t))</PRE
><P CLASS="j"
>The following function prints out every <A HREF="26_1_Glossary.html#present"
><EM CLASS="term"
>present</EM
></A
> <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> (possibly more than once):</P
><PRE CLASS="screen"
>(defun print-all-symbols ()
(with-package-iterator (next-symbol (list-all-packages)
:internal :external)
(loop
(multiple-value-bind (more? symbol) (next-symbol)
(if more?
(print symbol)
(return))))))</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
><A HREF="f_with-package-iterator.html" CLASS="macref"
><B
>with-package-iterator</B
></A
> signals an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_program-error.html" CLASS="typeref"
><B
>program-error</B
></A
> if no <VAR CLASS="param"
>symbol-types</VAR
> are supplied or if a <VAR CLASS="param"
>symbol-type</VAR
> is not recognized by the implementation is supplied. </P
><P CLASS="j"
>The consequences are undefined if the local function named <VAR CLASS="param"
>name</VAR
> <A HREF="26_1_Glossary.html#establish"
><EM CLASS="term"
>established</EM
></A
> by <A HREF="f_with-package-iterator.html" CLASS="macref"
><B
>with-package-iterator</B
></A
> is called after it has returned <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
> as its <A HREF="26_1_Glossary.html#primary_value"
><EM CLASS="term"
>primary value</EM
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="3_6_Traversal_Rules_and_Side_Effects.html#sec_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.6</SPAN
> <SPAN CLASS="cmr"
>(Traversal</SPAN
> <SPAN CLASS="cmr"
>Rules</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Side</SPAN
> <SPAN CLASS="cmr"
>Effects)</SPAN
></A
></P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_make-package.html" CLASS="prev"
>&#8592;</A
><A HREF="f_unexport.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>