1
0
Fork 0
cl-sites/novaspec.org/cl/t_function.html

595 lines
13 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>function | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="t_function.html"
><LINK REL="next" HREF="t_compiled-function.html" TYPE="text/html" TITLE="compiled-function"
><LINK REL="prev" HREF="t_boolean.html" TYPE="text/html" TITLE="boolean"
><LINK REL="up" HREF="4_4_Types_and_Classes_Dictionary.html" TYPE="text/html" TITLE="4.4 Types and Classes 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="4_Types_and_Classes.html"
>4. Types and Classes</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="4_4_Types_and_Classes_Dictionary.html"
>4.4 Types and Classes Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="t_function.html"
>function</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="t_boolean.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="t_compiled-function.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="function"
></SPAN
><B
>function</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>System</I
> <I
>Class</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Class Precedence List</B
></DT
><DD
><P CLASS="j"
><A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
>, <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
></P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
>A <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> is an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> that represents code to be executed when an appropriate number of arguments is supplied. A <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> is produced by the <A HREF="f_function.html" CLASS="specref"
><B
>function</B
></A
> <A HREF="26_1_Glossary.html#special_form"
><EM CLASS="term"
>special form</EM
></A
>, the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_coerce.html" CLASS="funref"
><B
>coerce</B
></A
>, or the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_compile.html" CLASS="funref"
><B
>compile</B
></A
>. A <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> can be directly invoked by using it as the first argument to <A HREF="f_funcall.html" CLASS="funref"
><B
>funcall</B
></A
>, <A HREF="f_apply.html" CLASS="funref"
><B
>apply</B
></A
>, or <A HREF="f_multiple-value-call.html" CLASS="specref"
><B
>multiple-value-call</B
></A
>.</P
></DD
><DT
><B
>Compound Type Specifier Kind</B
></DT
><DD
><P CLASS="j"
>Specializing.</P
></DD
><DT
><B
>Compound Type Specifier Syntax</B
></DT
><DD
><TABLE CELLSPACING="0" CELLPADDING="0" RULES="GROUPS" STYLE="margin: 1ex 0"
><TBODY
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";"
><SPAN CLASS="cmtt"
>(function</SPAN
> <SPAN CLASS="cmtt"
>[</SPAN
><SPAN CLASS="cmssi"
>arg-typespec</SPAN
> <SPAN CLASS="cmr"
>[</SPAN
><SPAN CLASS="cmssi"
>value-typespec</SPAN
><SPAN CLASS="cmr"
>]</SPAN
><SPAN CLASS="cmtt"
>])</SPAN
></TD
></TR
></TBODY
></TABLE
><TABLE CELLSPACING="0" CELLPADDING="0" RULES="GROUPS" STYLE="margin: 1ex 0"
><TBODY
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;padding-right:3px"
><VAR CLASS="param"
>arg-typespec</VAR
><SPAN CLASS="cmssi"
>::</SPAN
><SPAN CLASS="cmr"
>=</SPAN
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>typespec</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmtt"
>[&amp;optional</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>typespec</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
><SPAN CLASS="cmtt"
>]</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmtt"
>[&amp;rest</SPAN
> <VAR CLASS="param"
>typespec</VAR
><SPAN CLASS="cmtt"
>]</SPAN
></TD
></TR
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE=";padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;padding-right:3px"
></TD
><TD ALIGN="LEFT" VALIGN="BASELINE" STYLE="padding-left:3px;"
> <SPAN CLASS="cmtt"
>[&amp;key</SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmssi"
>keyword</SPAN
> <SPAN CLASS="cmssi"
>typespec</SPAN
><SPAN CLASS="cmr"
>)</SPAN
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
><SPAN CLASS="cmtt"
>]</SPAN
><SPAN CLASS="cmr"
>)</SPAN
></TD
></TR
></TBODY
></TABLE
></DD
><DT
><B
>Compound Type Specifier Arguments</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>typespec</VAR
> &#8212; a <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>value-typespec</VAR
> &#8212; a <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
>.</P
></DD
><DT
><B
>Compound Type Specifier Description</B
></DT
><DD
><P CLASS="j"
>The list form of the <A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
> <EM CLASS="term"
>type-specifier</EM
> can be used only for declaration and not for discrimination. Every element of this <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> is a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> that accepts arguments of the types specified by the <VAR CLASS="param"
>argj-types</VAR
> and returns values that are members of the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
> specified by <VAR CLASS="param"
>value-type</VAR
>. The <SPAN CLASS="keyref"
><B
>&amp;optional</B
></SPAN
>, <SPAN CLASS="keyref"
><B
>&amp;rest</B
></SPAN
>, <SPAN CLASS="keyref"
><B
>&amp;key</B
></SPAN
>, and <SPAN CLASS="keyref"
><B
>&amp;allow-other-keys</B
></SPAN
> markers can appear in the list of argument types. The <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
> provided with <SPAN CLASS="keyref"
><B
>&amp;rest</B
></SPAN
> is the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of each actual argument, not the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of the corresponding variable. </P
><P CLASS="j"
>The <SPAN CLASS="keyref"
><B
>&amp;key</B
></SPAN
> parameters should be supplied as lists of the form <SPAN CLASS="cmtt"
>(</SPAN
><VAR CLASS="param"
>keyword</VAR
> <VAR CLASS="param"
>type</VAR
><SPAN CLASS="cmtt"
>)</SPAN
>. The <VAR CLASS="param"
>keyword</VAR
> must be a valid keyword-name symbol as must be supplied in the actual arguments of a call. This is usually a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> in the <SPAN CLASS="packref"
><SPAN CLASS="cmtt"
>KEYWORD</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> but can be any <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>. When <SPAN CLASS="keyref"
><B
>&amp;key</B
></SPAN
> is given in a <SPAN CLASS="declref"
><B
>function</B
></SPAN
> <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
> <A HREF="26_1_Glossary.html#lambda_list"
><EM CLASS="term"
>lambda list</EM
></A
>, the <A HREF="26_1_Glossary.html#keyword_parameter"
><EM CLASS="term"
>keyword parameters</EM
></A
> given are exhaustive unless <SPAN CLASS="keyref"
><B
>&amp;allow-other-keys</B
></SPAN
> is also present. <SPAN CLASS="keyref"
><B
>&amp;allow-other-keys</B
></SPAN
> is an indication that other keyword arguments might actually be supplied and, if supplied, can be used. For example, the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> <A HREF="f_make-list.html" CLASS="funref"
><B
>make-list</B
></A
> could be declared as follows:</P
><PRE CLASS="screen"
>(function ((integer 0) &amp;key (:initial-element t)) list)</PRE
><P CLASS="j"
>The <VAR CLASS="param"
>value-type</VAR
> can be a <SPAN CLASS="declref"
><B
>values</B
></SPAN
> <A HREF="26_1_Glossary.html#type_specifier"
><EM CLASS="term"
>type specifier</EM
></A
> in order to indicate the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
> of <A HREF="26_1_Glossary.html#multiple_values"
><EM CLASS="term"
>multiple values</EM
></A
>. </P
><P CLASS="j"
>Consider a declaration of the following form:</P
><PRE CLASS="screen"
>(ftype (function (arg0-type arg1-type ...) val-type) f))</PRE
><P CLASS="j"
>Any <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> <SPAN CLASS="cmtt"
>(f</SPAN
> <SPAN CLASS="cmtt"
>arg0</SPAN
> <SPAN CLASS="cmtt"
>arg1</SPAN
> <SPAN CLASS="cmtt"
>...)</SPAN
> within the scope of that declaration is equivalent to the following:</P
><PRE CLASS="screen"
>(the val-type (f (the arg0-type arg0) (the arg1-type arg1) ...))</PRE
><P CLASS="j"
>That is, the consequences are undefined if any of the arguments are not of the specified <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
> or the result is not of the specified <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. In particular, if any argument is not of the correct <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>, the result is not guaranteed to be of the specified <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. </P
><P CLASS="j"
>Thus, an <A HREF="m_ftype.html" CLASS="declref"
><B
>ftype</B
></A
> declaration for a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> describes <A HREF="26_1_Glossary.html#call"
><EM CLASS="term"
>calls</EM
></A
> to the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
>, not the actual definition of the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
>. </P
><P CLASS="j"
>Consider a declaration of the following form:</P
><PRE CLASS="screen"
>(type (function (arg0-type arg1-type ...) val-type) fn-valued-variable)</PRE
><P CLASS="j"
>This declaration has the interpretation that, within the scope of the declaration, the consequences are unspecified if the value of <SPAN CLASS="cmtt"
>fn-valued-variable</SPAN
> is called with arguments not of the specified <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
>; the value resulting from a valid call will be of type <SPAN CLASS="cmtt"
>val-type</SPAN
>. </P
><P CLASS="j"
>As with variable type declarations, nested declarations imply intersections of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
>, as follows:</P
><UL
><LI
><P CLASS="j"
>Consider the following two declarations of <A HREF="m_ftype.html" CLASS="declref"
><B
>ftype</B
></A
>:</P
><PRE CLASS="screen"
>(ftype (function (arg0-type1 arg1-type1 ...) val-type1) f))</PRE
><P CLASS="j"
>and</P
><PRE CLASS="screen"
>(ftype (function (arg0-type2 arg1-type2 ...) val-type2) f))</PRE
><P CLASS="j"
>If both these declarations are in effect, then within the shared scope of the declarations, calls to <SPAN CLASS="cmtt"
>f</SPAN
> can be treated as if <SPAN CLASS="cmtt"
>f</SPAN
> were declared as follows:</P
><PRE CLASS="screen"
>(ftype (function ((and arg0-type1 arg0-type2) (and arg1-type1 arg1-type2 ...) ...)
(and val-type1 val-type2))
f))</PRE
><P CLASS="j"
>It is permitted to ignore one or all of the <A HREF="m_ftype.html" CLASS="declref"
><B
>ftype</B
></A
> declarations in force.</P
></LI
><LI
><P CLASS="j"
>If two (or more) type declarations are in effect for a variable, and they are both <SPAN CLASS="cmtt"
>function</SPAN
> declarations, the declarations combine similarly.</P
></LI
></UL
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="t_boolean.html" CLASS="prev"
>&#8592;</A
><A HREF="t_compiled-function.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
>