61 lines
59 KiB
HTML
61 lines
59 KiB
HTML
|
<!doctype html>
|
|||
|
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-chap-3/d-d-declarations" data-has-hydrated="false">
|
|||
|
<head>
|
|||
|
<meta charset="UTF-8">
|
|||
|
<meta name="generator" content="Docusaurus v3.0.1">
|
|||
|
<title data-rh="true">3.3 Declarations | Common Lisp (New) Language Reference</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" name="twitter:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" property="og:url" content="https://lisp-docs.github.io/cl-language-reference/chap-3/d-d-declarations"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="google-site-verification" content="Vzaw013_bfdKeUVG89Ch3W1zC9_vH9ID2dPB9Dz0vr0"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="3.3 Declarations | Common Lisp (New) Language Reference"><meta data-rh="true" name="description" content="3.3.1 Minimal Declaration Processing Requirements"><meta data-rh="true" property="og:description" content="3.3.1 Minimal Declaration Processing Requirements"><link data-rh="true" rel="icon" href="../img/favicon.ico"><link data-rh="true" rel="canonical" href="d-d-declarations.html"><link data-rh="true" rel="alternate" href="d-d-declarations.html" hreflang="en"><link data-rh="true" rel="alternate" href="d-d-declarations.html" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://C1F2Q5VM6X-dsn.algolia.net" crossorigin="anonymous"><link rel="preconnect" href="https://www.google-analytics.com">
|
|||
|
<link rel="preconnect" href="https://www.googletagmanager.com">
|
|||
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8TJCE4NSF8"></script>
|
|||
|
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-8TJCE4NSF8",{})</script>
|
|||
|
|
|||
|
|
|||
|
<link rel="search" type="application/opensearchdescription+xml" title="Common Lisp (New) Language Reference" href="../opensearch.xml"><link rel="stylesheet" href="../assets/css/styles.f13b59fe.css">
|
|||
|
<script src="../assets/js/runtime~main.02699c25.js" defer="defer"></script>
|
|||
|
<script src="../assets/js/main.4f0a7a76.js" defer="defer"></script>
|
|||
|
</head>
|
|||
|
<body class="navigation-with-keyboard">
|
|||
|
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="d-d-declarations.html#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a href="../../index.html" target="_blank" rel="noopener noreferrer" class="navbar__brand"><div class="navbar__logo"><img src="../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Common Lisp Docs</b></a><a href="../../docs/tutorial/index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Tutorial</a><a href="../index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Technical Reference</a><a href="../../docs/whylisp.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Why Lisp?</a><a href="../../docs/howto.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Guides</a></div><div class="navbar__items navbar__items--right"><a href="../../docs/contribute.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Contribute!</a><a href="../../docs/help.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Getting Help</a><a href="../../docs/about.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">About</a><a href="../../blog.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Blog</a><a href="https://github.com/lisp-docs" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-live="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.
|
|||
|
<!-- -->
|
|||
|
<p><span><i>Declarations</i></span> provide a way of specifying information for use by program processors, such as the evaluator or the compiler.</p>
|
|||
|
<p><span><i>Local declarations</i></span> can be embedded in executable code using <span><b>declare</b></span>. <span><i>Global declarations</i></span>, or <span><i>proclamations</i></span>, are established by <span><b>proclaim</b></span> or <span><b>declaim</b></span>.</p>
|
|||
|
<p>The <span><b>the</b></span> <span><i>special form</i></span> provides a shorthand notation for making a <span><i>local declaration</i></span> about the <span><i>type</i></span> of the <span><i>value</i></span> of a given <span><i>form</i></span>.</p>
|
|||
|
<p>The consequences are undefined if a program violates a <span><i>declaration</i></span> or a <span><i>proclamation</i></span>.</p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="331-minimal-declaration-processing-requirements">3.3.1 Minimal Declaration Processing Requirements<a href="d-d-declarations.html#331-minimal-declaration-processing-requirements" class="hash-link" aria-label="Direct link to 3.3.1 Minimal Declaration Processing Requirements" title="Direct link to 3.3.1 Minimal Declaration Processing Requirements"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>In general, an <span><i>implementation</i></span> is free to ignore <span><i>declaration specifiers</i></span> except for the <span><b>declaration</b></span>, <span><b>notinline</b></span>, <strong>safety</strong>, and <span><b>special</b></span> <span><i>declaration specifiers</i></span>.</p>
|
|||
|
<p>A <span><b>declaration</b></span> <span><i>declaration</i></span> must suppress warnings about unrecognized <span><i>declarations</i></span> of the kind that it declares. If an <span><i>implementation</i></span> does not produce warnings about unrecognized declarations, it may safely ignore this <span><i>declaration</i></span>.</p>
|
|||
|
<p>A <span><b>notinline</b></span> <span><i>declaration</i></span> must be recognized by any <span><i>implementation</i></span> that supports inline functions or <span><i>compiler macros</i></span> in order to disable those facilities. An <span><i>implementation</i></span> that does not use inline functions or <span><i>compiler macros</i></span> may safely ignore this <span><i>declaration</i></span>.</p>
|
|||
|
<p>A <strong>safety</strong> <span><i>declaration</i></span> that increases the current safety level must always be recognized. An <span><i>implementation</i></span> that always processes code as if safety were high may safely ignore this <span><i>declaration</i></span>.</p>
|
|||
|
<p>A <span><b>special</b></span> <span><i>declaration</i></span> must be processed by all <span><i>implementations</i></span>.</p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="332-declaration-specifiers">3.3.2 Declaration Specifiers<a href="d-d-declarations.html#332-declaration-specifiers" class="hash-link" aria-label="Direct link to 3.3.2 Declaration Specifiers" title="Direct link to 3.3.2 Declaration Specifiers"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>A <span><i>declaration specifier</i></span> is an <span><i>expression</i></span> that can appear at top level of a <span><b>declare</b></span> expression or a <span><b>declaim</b></span> form, or as the argument to <span><b>proclaim</b></span>. It is a <span><i>list</i></span> whose <span><i>car</i></span> is a <span><i>declaration identifier</i></span> , and whose <span><i>cdr</i></span> is data interpreted according to rules specific to the <span><i>declaration identifier</i></span> .</p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="333-declaration-identifiers">3.3.3 Declaration Identifiers<a href="d-d-declarations.html#333-declaration-identifiers" class="hash-link" aria-label="Direct link to 3.3.3 Declaration Identifiers" title="Direct link to 3.3.3 Declaration Identifiers"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>Figure 3–9 shows a list of all <span><i>declaration identifiers</i></span> defined by this standard.</p>
|
|||
|
<p>|</p><p><strong>declaration ignore special</strong> </p><p><strong>dynamic-extent inline type</strong> </p><p><strong>ftype notinline</strong> </p><p><strong>ignorable optimize</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 3–9. Common Lisp Declaration Identifiers</strong></p>
|
|||
|
<p>An implementation is free to support other (<span><i>implementation-defined</i></span>) <span><i>declaration identifiers</i></span> as well. A warning might be issued if a <span><i>declaration identifier</i></span> is not among those defined above, is not defined by the <span><i>implementation</i></span>, is not a <em>type name</em>, and has not been declared in a <span><b>declaration</b></span> <span><i>proclamation</i></span>.</p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="3331-shorthand-notation-for-type-declarations">3.3.3.1 Shorthand notation for Type Declarations<a href="d-d-declarations.html#3331-shorthand-notation-for-type-declarations" class="hash-link" aria-label="Direct link to 3.3.3.1 Shorthand notation for Type Declarations" title="Direct link to 3.3.3.1 Shorthand notation for Type Declarations"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>A <span><i>type specifier</i></span> can be used as a <span><i>declaration identifier</i></span> . (<em>type-specifier {var}</em>*) is taken as shorthand for (type <em>type-specifier {var}</em>*).</p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="334-declaration-scope">3.3.4 Declaration Scope<a href="d-d-declarations.html#334-declaration-scope" class="hash-link" aria-label="Direct link to 3.3.4 Declaration Scope" title="Direct link to 3.3.4 Declaration Scope"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p><span><i>Declarations</i></span> can be divided into two kinds: those that apply to the <span><i>bindings</i></span> of <span><i>variables</i></span> or <span><i>functions</i></span>; and those that do not apply to <span><i>bindings</i></span>.</p>
|
|||
|
<p>A <span><i>declaration</i></span> that appears at the head of a binding <span><i>form</i></span> and applies to a <span><i>variable</i></span> or <em>function binding</em> made by that <span><i>form</i></span> is called a <span><i>bound declaration</i></span>; such a <span><i>declaration</i></span> affects both the <span><i>binding</i></span> and any references within the <span><i>scope</i></span> of the <span><i>declaration</i></span>.</p>
|
|||
|
<p><span><i>Declarations</i></span> that are not <span><i>bound declarations</i></span> are called <span><i>free declarations</i></span>.</p>
|
|||
|
<p>A <span><i>free declaration</i></span> in a <em>form F</em>1 that applies to a <span><i>binding</i></span> for a <em>name N established</em> by some <em>form F</em>2 of which <em>F</em>1 is a <span><i>subform</i></span> affects only references to <em>N</em> within <em>F</em>1; it does not to apply to other references to <em>N</em> outside of <em>F</em>1, nor does it affect the manner in which the <span><i>binding</i></span> of <em>N</em> by <em>F</em>2 is <em>established</em>.</p>
|
|||
|
<p><span><i>Declarations</i></span> that do not apply to <span><i>bindings</i></span> can only appear as <span><i>free declarations</i></span>.</p>
|
|||
|
<p>The <span><i>scope</i></span> of a <span><i>bound declaration</i></span> is the same as the <span><i>lexical scope</i></span> of the <span><i>binding</i></span> to which it applies; for <span><i>special variables</i></span>, this means the <span><i>scope</i></span> that the <span><i>binding</i></span> would have had had it been a <span><i>lexical binding</i></span>.</p>
|
|||
|
<p>Unless explicitly stated otherwise, the <span><i>scope</i></span> of a <span><i>free declaration</i></span> includes only the body <span><i>subforms</i></span> of the <span><i>form</i></span> at whose head it appears, and no other <span><i>subforms</i></span>. The <span><i>scope</i></span> of <span><i>free declarations</i></span> specifically does not include <span><i>initialization forms</i></span> for <span><i>bindings</i></span> established by the <span><i>form</i></span> containing the <span><i>declarations</i></span>.</p>
|
|||
|
<p>Some <span><i>iteration forms</i></span> include step, end-test, or result <span><i>subforms</i></span> that are also included in the <span><i>scope</i></span> of <span><i>declarations</i></span> that appear in the <span><i>iteration form</i></span>. Specifically, the <span><i>iteration forms</i></span> and <span><i>subforms</i></span> involved are:</p>
|
|||
|
<p><em>•</em> <span><b>do</b></span>, <span><b>do*</b></span>: <em>step-forms</em>, <em>end-test-form</em>, and <em>result-forms</em>.</p>
|
|||
|
<p><em>•</em> <span><b>dolist</b></span>, <span><b>dotimes</b></span>: <em>result-form</em></p>
|
|||
|
<p><em>•</em> <span><b>do-all-symbols</b></span>, <span><b>do-external-symbols</b></span>, <span><b>do-symbols</b></span>: <em>result-form</em></p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="3341-examples-of-declaration-scope">3.3.4.1 Examples of Declaration Scope<a href="d-d-declarations.html#3341-examples-of-declaration-scope" class="hash-link" aria-label="Direct link to 3.3.4.1 Examples of Declaration Scope" title="Direct link to 3.3.4.1 Examples of Declaration Scope"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<!-- -->
|
|||
|
<div class="language-lisp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-lisp codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">Here is an example illustrating the *scope* of *bound declarations*. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">let</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">x</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;[1] 1st occurrence of x </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token declare keyword" style="color:#00009f">declare</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">special</span><span class="token plain"> x</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;[2] 2nd occurrence of x </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">let</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">x</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">2</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;[3] 3rd occurrence of x </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">let</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">old-x</span><span class="token plain"> x</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;[4] 4th occurrence of x </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">x</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">3</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;[5] 5th occurrence of x </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="toke
|
|||
|
</body>
|
|||
|
</html>
|