emacs.d/clones/lisp/HyperSpec-7-0/HyperSpec/Body/f_coerce.htm
2023-01-18 20:30:47 +01:00

104 lines
13 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Function COERCE</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="t_eql.htm">
<LINK REL=UP HREF="c_types_.htm">
<LINK REL=NEXT HREF="m_deftp.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="t_eql.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_types_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_deftp.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="coerce"><I>Function</I> <B>COERCE</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>coerce</B> <I>object result-type</I> =&gt; <I>result</I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>object</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
<I>result-type</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>. <P>
<I>result</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>, of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <I>result-type</I> except in situations described in <A REL=CHILD HREF="12_aec.htm">Section 12.1.5.3 (Rule of Canonical Representation for Complex Rationals)</A>. <P>
<P><B>Description:</B><P>
<P>
<A REL=DEFINITION HREF="26_glo_c.htm#coerce"><I>Coerces</I></A> the <I>object</I> to <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <I>result-type</I>. <P>
If <I>object</I> is already of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <I>result-type</I>, the <I>object</I> itself is returned, regardless of whether it would have been possible in general to coerce an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> of some other <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> to <I>result-type</I>. <P>
Otherwise, the <I>object</I> is <I>coerced</I> to <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <I>result-type</I> according to the following rules: <P>
<P><DL><P>
<DT><A REL=DEFINITION HREF="t_seq.htm#sequence"><B>sequence</B></A> <P><DD>
<P>
If the <I>result-type</I> is a <A REL=DEFINITION HREF="26_glo_r.htm#recognizable_subtype"><I>recognizable subtype</I></A> of <A REL=DEFINITION HREF="t_list.htm#list"><B>list</B></A>, and the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A>, then the <I>result</I> is a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> that has the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>elements</I></A> as <I>object</I>. <P>
If the <I>result-type</I> is a <A REL=DEFINITION HREF="26_glo_r.htm#recognizable_subtype"><I>recognizable subtype</I></A> of <A REL=DEFINITION HREF="t_vector.htm#vector"><B>vector</B></A>, and the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A>, then the <I>result</I> is a <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A> that has the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>elements</I></A> as <I>object</I>. If <I>result-type</I> is a specialized <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>, the <I>result</I> has an <A REL=DEFINITION HREF="26_glo_a.htm#actual_array_element_type"><I>actual array element type</I></A> that is the result of <I>upgrading</I> the element type part of that <A REL=DEFINITION HREF="26_glo_s.htm#specialized"><I>specialized</I></A> <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>. If no element type is specified, the element type defaults to <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>. If the <A REL=DEFINITION HREF="26_glo_i.htm#implementation"><I>implementation</I></A> cannot determine the element type, an error is signaled. <P>
<P>
<DT><A REL=DEFINITION HREF="t_ch.htm#character"><B>character</B></A> <P><DD>
If the <I>result-type</I> is <A REL=DEFINITION HREF="t_ch.htm#character"><B>character</B></A> and the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is a <A REL=DEFINITION HREF="26_glo_c.htm#character_designator"><I>character designator</I></A>, the <I>result</I> is the <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> it denotes. <P>
<P>
<DT><A REL=DEFINITION HREF="t_comple.htm#complex"><B>complex</B></A> <P><DD>
If the <I>result-type</I> is <A REL=DEFINITION HREF="t_comple.htm#complex"><B>complex</B></A> and the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is a <A REL=DEFINITION HREF="t_real.htm#real"><I>real</I></A>, then the <I>result</I> is obtained by constructing a <A REL=DEFINITION HREF="26_glo_c.htm#complex"><I>complex</I></A> whose real part is the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> and whose imaginary part is the result of <I>coercing</I> an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A> zero to the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> (using <A REL=DEFINITION HREF="#coerce"><B>coerce</B></A>). (If the real part is a <A REL=DEFINITION HREF="26_glo_r.htm#rational"><I>rational</I></A>, however, then the result must be represented as a <A REL=DEFINITION HREF="26_glo_r.htm#rational"><I>rational</I></A> rather than a <A REL=DEFINITION HREF="26_glo_c.htm#complex"><I>complex</I></A>; see <A REL=CHILD HREF="12_aec.htm">Section 12.1.5.3 (Rule of Canonical Representation for Complex Rationals)</A>. So, for example, <TT>(coerce 3 'complex)</TT> is permissible, but will return <TT>3</TT>, which is not a <A REL=DEFINITION HREF="26_glo_c.htm#complex"><I>complex</I></A>.) <P>
<DT><A REL=DEFINITION HREF="t_float.htm#float"><B>float</B></A> <P><DD>
If the <I>result-type</I> is any of <A REL=DEFINITION HREF="t_float.htm#float"><B>float</B></A>, <A REL=DEFINITION HREF="t_short_.htm#short-float"><B>short-float</B></A>, <A REL=DEFINITION HREF="t_short_.htm#single-float"><B>single-float</B></A>, <A REL=DEFINITION HREF="t_short_.htm#double-float"><B>double-float</B></A>, <A REL=DEFINITION HREF="t_short_.htm#long-float"><B>long-float</B></A>, and the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is a <A REL=DEFINITION HREF="t_real.htm#real"><I>real</I></A>, then the <I>result</I> is a <A REL=DEFINITION HREF="26_glo_f.htm#float"><I>float</I></A> of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <I>result-type</I> which is equal in sign and magnitude to the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> to whatever degree of representational precision is permitted by that <A REL=DEFINITION HREF="26_glo_f.htm#float"><I>float</I></A> representation. (If the <I>result-type</I> is <A REL=DEFINITION HREF="t_float.htm#float"><B>float</B></A> and <I>object</I> is not already a <A REL=DEFINITION HREF="26_glo_f.htm#float"><I>float</I></A>, then the <I>result</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#single_float"><I>single float</I></A>.) <P>
<DT><A REL=DEFINITION HREF="t_fn.htm#function"><B>function</B></A> <P><DD>
If the <I>result-type</I> is <A REL=DEFINITION HREF="t_fn.htm#function"><B>function</B></A>, and <I>object</I> is any <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A> that is <A REL=DEFINITION HREF="26_glo_f.htm#fbound"><I>fbound</I></A> but that is globally defined neither as a <A REL=DEFINITION HREF="26_glo_m.htm#macro_name"><I>macro name</I></A> nor as a <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A>, then the <I>result</I> is the <A REL=DEFINITION HREF="26_glo_f.htm#functional_value"><I>functional value</I></A> of <I>object</I>. <P>
If the <I>result-type</I> is <A REL=DEFINITION HREF="t_fn.htm#function"><B>function</B></A>, and <I>object</I> is a <A REL=DEFINITION HREF="26_glo_l.htm#lambda_expression"><I>lambda expression</I></A>, then the <I>result</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#closure"><I>closure</I></A> of <I>object</I> in the <A REL=DEFINITION HREF="26_glo_n.htm#null_lexical_environment"><I>null lexical environment</I></A>. <P>
<DT><A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A> <P><DD>
Any <I>object</I> can be <I>coerced</I> to an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>. In this case, the <I>object</I> is simply returned. <P>
<P></DL><P>
<P><B>Examples:</B><P>
<P>
<PRE>
(coerce '(a b c) 'vector) =&gt; #(A B C)
(coerce 'a 'character) =&gt; #\A
(coerce 4.56 'complex) =&gt; #C(4.56 0.0)
(coerce 4.5s0 'complex) =&gt; #C(4.5s0 0.0s0)
(coerce 7/2 'complex) =&gt; 7/2
(coerce 0 'short-float) =&gt; 0.0s0
(coerce 3.5L0 'float) =&gt; 3.5L0
(coerce 7/2 'float) =&gt; 3.5
(coerce (cons 1 2) t) =&gt; (1 . 2)
</PRE>
</TT> <P>
All the following <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> should signal an error: <P>
<PRE>
(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 &quot;foo&quot; '(string 2))
(coerce #(#\a #\b #\c) '(string 2))
(coerce '(0 1) '(simple-bit-vector 3))
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B><P>
<P>
If a coercion is not possible, an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> is signaled. <P>
<TT>(coerce x 'nil)</TT> always signals an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A>. <P>
An error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_error.htm#error"><B>error</B></A> is signaled if the <I>result-type</I> is <A REL=DEFINITION HREF="t_fn.htm#function"><B>function</B></A> but <I>object</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> that is not <A REL=DEFINITION HREF="26_glo_f.htm#fbound"><I>fbound</I></A> or if the <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> names a <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> or a <A REL=DEFINITION HREF="26_glo_s.htm#special_operator"><I>special operator</I></A>. <P>
An error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> should be signaled if <I>result-type</I> specifies the number of elements and <I>object</I> is of a different length. <P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="f_ration.htm#rational"><B>rational</B></A>, <A REL=DEFINITION HREF="f_floorc.htm#floor"><B>floor</B></A>, <A REL=DEFINITION HREF="f_char_c.htm#char-code"><B>char-code</B></A>, <A REL=DEFINITION HREF="f_char_i.htm#char-int"><B>char-int</B></A> <P>
<P><B>Notes:</B><P>
<P>
Coercions from <A REL=DEFINITION HREF="26_glo_f.htm#float"><I>floats</I></A> to <A REL=DEFINITION HREF="26_glo_r.htm#rational"><I>rationals</I></A> and from <A REL=DEFINITION HREF="26_glo_r.htm#ratio"><I>ratios</I></A> to <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integers</I></A> are not provided because of rounding problems. <P>
<PRE>
(coerce x 't) == (identity x) == x
</PRE>
</TT> <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss302.htm">SEQUENCE-TYPE-LENGTH:MUST-MATCH</A><LI> <A REL=CHILD HREF="../Issues/iss054.htm">COERCING-SETF-NAME-TO-FUNCTION:ALL-FUNCTION-NAMES</A><LI> <A REL=CHILD HREF="../Issues/iss179.htm">FUNCTION-TYPE:X3J13-MARCH-88</A><LI> <A REL=CHILD HREF="../Issues/iss290.htm">REAL-NUMBER-TYPE:X3J13-MAR-89</A><LI> <A REL=CHILD HREF="../Issues/iss025.htm">CHARACTER-LOOSE-ENDS:FIX</A><LI> <A REL=CHILD HREF="../Issues/iss073.htm">CONCATENATE-SEQUENCE:SIGNAL-ERROR</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>