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

839 lines
No EOL
16 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>coerce | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_coerce.html"
><LINK REL="next" HREF="f_deftype.html" TYPE="text/html" TITLE="deftype"
><LINK REL="prev" HREF="t_eql.html" TYPE="text/html" TITLE="eql"
><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="f_coerce.html"
>coerce</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="t_eql.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_deftype.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="coerce"
></SPAN
><B
>coerce</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>coerce</B
> <SPAN CLASS="cmssi"
>object</SPAN
> <SPAN CLASS="cmssi"
>result-type</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>result</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>object</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>result-type</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"
>result</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>, of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <VAR CLASS="param"
>result-type</VAR
> except in situations described in <A HREF="12_1_Number_Concepts.html#sec_12_1_5_3" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>12.1.5.3</SPAN
> <SPAN CLASS="cmr"
>(Rule</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>Canonical</SPAN
> <SPAN CLASS="cmr"
>Representation</SPAN
> <SPAN CLASS="cmr"
>for</SPAN
> <SPAN CLASS="cmr"
>Complex</SPAN
> <SPAN CLASS="cmr"
>Rationals)</SPAN
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="26_1_Glossary.html#coerce"
><EM CLASS="term"
>Coerces</EM
></A
> the <VAR CLASS="param"
>object</VAR
> to <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <VAR CLASS="param"
>result-type</VAR
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>object</VAR
> is already of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <VAR CLASS="param"
>result-type</VAR
>, the <VAR CLASS="param"
>object</VAR
> itself is returned, regardless of whether it would have been possible in general to coerce an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> of some other <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> to <VAR CLASS="param"
>result-type</VAR
>. </P
><P CLASS="j"
>Otherwise, the <VAR CLASS="param"
>object</VAR
> is <A HREF="26_1_Glossary.html#coerce"
><EM CLASS="term"
>coerced</EM
></A
> to <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <VAR CLASS="param"
>result-type</VAR
> according to the following rules:</P
><DL
><DT
><A HREF="t_sequence.html" CLASS="typeref"
><B
>sequence</B
></A
></DT
><DD
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is a <A HREF="26_1_Glossary.html#recognizable_subtype"
><EM CLASS="term"
>recognizable subtype</EM
></A
> of <A HREF="t_list.html" CLASS="typeref"
><B
>list</B
></A
>, and the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is a <A HREF="26_1_Glossary.html#sequence"
><EM CLASS="term"
>sequence</EM
></A
>, then the <VAR CLASS="param"
>result</VAR
> is a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> that has the <A HREF="26_1_Glossary.html#same"
><EM CLASS="term"
>same</EM
></A
> <A HREF="26_1_Glossary.html#element"
><EM CLASS="term"
>elements</EM
></A
> as <VAR CLASS="param"
>object</VAR
>. </P
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is a <A HREF="26_1_Glossary.html#recognizable_subtype"
><EM CLASS="term"
>recognizable subtype</EM
></A
> of <A HREF="t_vector.html" CLASS="typeref"
><B
>vector</B
></A
>, and the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is a <A HREF="26_1_Glossary.html#sequence"
><EM CLASS="term"
>sequence</EM
></A
>, then the <VAR CLASS="param"
>result</VAR
> is a <A HREF="26_1_Glossary.html#vector"
><EM CLASS="term"
>vector</EM
></A
> that has the <A HREF="26_1_Glossary.html#same"
><EM CLASS="term"
>same</EM
></A
> <A HREF="26_1_Glossary.html#element"
><EM CLASS="term"
>elements</EM
></A
> as <VAR CLASS="param"
>object</VAR
>. If <VAR CLASS="param"
>result-type</VAR
> is a specialized <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>, the <VAR CLASS="param"
>result</VAR
> has an <A HREF="26_1_Glossary.html#actual_array_element_type"
><EM CLASS="term"
>actual array element type</EM
></A
> that is the result of <A HREF="26_1_Glossary.html#upgrade"
><EM CLASS="term"
>upgrading</EM
></A
> the element type part of that <A HREF="26_1_Glossary.html#specialized"
><EM CLASS="term"
>specialized</EM
></A
> <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
>. If no element type is specified, the element type defaults to <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
>. If the <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
> cannot determine the element type, an error is signaled.</P
></DD
><DT
><A HREF="t_character.html" CLASS="typeref"
><B
>character</B
></A
></DT
><DD
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_character.html" CLASS="typeref"
><B
>character</B
></A
> and the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is a <A HREF="26_1_Glossary.html#character_designator"
><EM CLASS="term"
>character designator</EM
></A
>, the <VAR CLASS="param"
>result</VAR
> is the <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
> it denotes.</P
></DD
><DT
><A HREF="t_complex.html" CLASS="typeref"
><B
>complex</B
></A
></DT
><DD
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_complex.html" CLASS="typeref"
><B
>complex</B
></A
> and the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is a <EM CLASS="term"
>real</EM
>, then the <VAR CLASS="param"
>result</VAR
> is obtained by constructing a <A HREF="26_1_Glossary.html#complex"
><EM CLASS="term"
>complex</EM
></A
> whose real part is the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> and whose imaginary part is the result of <A HREF="26_1_Glossary.html#coerce"
><EM CLASS="term"
>coercing</EM
></A
> an <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integer</EM
></A
> zero to the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> (using <A HREF="f_coerce.html" CLASS="funref"
><B
>coerce</B
></A
>). (If the real part is a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
>, however, then the result must be represented as a <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rational</EM
></A
> rather than a <A HREF="26_1_Glossary.html#complex"
><EM CLASS="term"
>complex</EM
></A
>; see <A HREF="12_1_Number_Concepts.html#sec_12_1_5_3" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>12.1.5.3</SPAN
> <SPAN CLASS="cmr"
>(Rule</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>Canonical</SPAN
> <SPAN CLASS="cmr"
>Representation</SPAN
> <SPAN CLASS="cmr"
>for</SPAN
> <SPAN CLASS="cmr"
>Complex</SPAN
> <SPAN CLASS="cmr"
>Rationals)</SPAN
></A
>. So, for example, <CODE CLASS="f"
>(coerce 3 'complex)</CODE
> is permissible, but will return <CODE CLASS="f"
>3</CODE
>, which is not a <A HREF="26_1_Glossary.html#complex"
><EM CLASS="term"
>complex</EM
></A
>.)</P
></DD
><DT
><A HREF="t_float.html" CLASS="typeref"
><B
>float</B
></A
></DT
><DD
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is any of <A HREF="t_float.html" CLASS="typeref"
><B
>float</B
></A
>, <A HREF="t_short-float.html" CLASS="typeref"
><B
>short-float</B
></A
>, <A HREF="t_short-float.html" CLASS="typeref"
><B
>single-float</B
></A
>, <A HREF="t_short-float.html" CLASS="typeref"
><B
>double-float</B
></A
>, <A HREF="t_short-float.html" CLASS="typeref"
><B
>long-float</B
></A
>, and the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is a <EM CLASS="term"
>real</EM
>, then the <VAR CLASS="param"
>result</VAR
> is a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <VAR CLASS="param"
>result-type</VAR
> which is equal in sign and magnitude to the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> to whatever degree of representational precision is permitted by that <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
> representation. (If the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_float.html" CLASS="typeref"
><B
>float</B
></A
> and <VAR CLASS="param"
>object</VAR
> is not already a <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>float</EM
></A
>, then the <VAR CLASS="param"
>result</VAR
> is a <A HREF="26_1_Glossary.html#single_float"
><EM CLASS="term"
>single float</EM
></A
>.)</P
></DD
><DT
><A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
></DT
><DD
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
>, and <VAR CLASS="param"
>object</VAR
> is any <A HREF="26_1_Glossary.html#function_name"
><EM CLASS="term"
>function name</EM
></A
> that is <A HREF="26_1_Glossary.html#fbound"
><EM CLASS="term"
>fbound</EM
></A
> but that is globally defined neither as a <A HREF="26_1_Glossary.html#macro_name"
><EM CLASS="term"
>macro name</EM
></A
> nor as a <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
>, then the <VAR CLASS="param"
>result</VAR
> is the <A HREF="26_1_Glossary.html#functional_value"
><EM CLASS="term"
>functional value</EM
></A
> of <VAR CLASS="param"
>object</VAR
>. </P
><P CLASS="j"
>If the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
>, and <VAR CLASS="param"
>object</VAR
> is a <A HREF="26_1_Glossary.html#lambda_expression"
><EM CLASS="term"
>lambda expression</EM
></A
>, then the <VAR CLASS="param"
>result</VAR
> is a <A HREF="26_1_Glossary.html#closure"
><EM CLASS="term"
>closure</EM
></A
> of <VAR CLASS="param"
>object</VAR
> in the <A HREF="26_1_Glossary.html#null_lexical_environment"
><EM CLASS="term"
>null lexical environment</EM
></A
>.</P
></DD
><DT
><A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
></DT
><DD
><P CLASS="j"
>Any <VAR CLASS="param"
>object</VAR
> can be <A HREF="26_1_Glossary.html#coerce"
><EM CLASS="term"
>coerced</EM
></A
> to an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
>. In this case, the <VAR CLASS="param"
>object</VAR
> is simply returned.</P
></DD
></DL
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(coerce '(a b c) 'vector) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #(A B C)
(coerce 'a 'character) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #\A
(coerce 4.56 'complex) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #C(4.56 0.0)
(coerce 4.5s0 'complex) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #C(4.5s0 0.0s0)
(coerce 7/2 'complex) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 7/2
(coerce 0 'short-float) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 0.0s0
(coerce 3.5L0 'float) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3.5L0
(coerce 7/2 'float) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3.5
(coerce (cons 1 2) t) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 . 2)</PRE
><P CLASS="j"
>All the following <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>forms</EM
></A
> should signal an error:</P
><PRE CLASS="screen"
>(coerce '(a b c) '(vector * 4))
(coerce #(a b c) '(vector * 4))
(coerce '(a b c) '(vector * 2))
(coerce #(a b c) '(vector * 2))
(coerce "foo" '(string 2))
(coerce #(#\a #\b #\c) '(string 2))
(coerce '(0 1) '(simple-bit-vector 3))</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>If a coercion is not possible, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> is signaled. </P
><P CLASS="j"
><CODE CLASS="f"
>(coerce x 'nil)</CODE
> always signals an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
>. </P
><P CLASS="j"
>An error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_error.html" CLASS="typeref"
><B
>error</B
></A
> is signaled if the <VAR CLASS="param"
>result-type</VAR
> is <A HREF="t_function.html" CLASS="typeref"
><B
>function</B
></A
> but <VAR CLASS="param"
>object</VAR
> is a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> that is not <A HREF="26_1_Glossary.html#fbound"
><EM CLASS="term"
>fbound</EM
></A
> or if the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> names a <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
> or a <A HREF="26_1_Glossary.html#special_operator"
><EM CLASS="term"
>special operator</EM
></A
>. </P
><P CLASS="j"
>An error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_type-error.html" CLASS="typeref"
><B
>type-error</B
></A
> should be signaled if <VAR CLASS="param"
>result-type</VAR
> specifies the number of elements and <VAR CLASS="param"
>object</VAR
> is of a different length.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_rational.html" CLASS="funref"
><B
>rational</B
></A
>, <A HREF="f_floor.html" CLASS="funref"
><B
>floor</B
></A
>, <A HREF="f_char-code.html" CLASS="funref"
><B
>char-code</B
></A
>, <A HREF="f_char-int.html" CLASS="funref"
><B
>char-int</B
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>Coercions from <A HREF="26_1_Glossary.html#float"
><EM CLASS="term"
>floats</EM
></A
> to <A HREF="26_1_Glossary.html#rational"
><EM CLASS="term"
>rationals</EM
></A
> and from <A HREF="26_1_Glossary.html#ratio"
><EM CLASS="term"
>ratios</EM
></A
> to <A HREF="26_1_Glossary.html#integer"
><EM CLASS="term"
>integers</EM
></A
> are not provided because of rounding problems.</P
><PRE CLASS="screen"
>(coerce x 't) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> (identity x) <SPAN CLASS="cmsy"
><SPAN STYLE="font-size:16px;vertical-align:-2px"
>&#8801;</SPAN
></SPAN
> x</PRE
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="t_eql.html" CLASS="prev"
>&#8592;</A
><A HREF="f_deftype.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
>