136 lines
12 KiB
HTML
136 lines
12 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: Macro TYPECASE, CTYPECASE, ETYPECASE</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="m_case_.htm">
|
|
<LINK REL=UP HREF="c_data_a.htm">
|
|
<LINK REL=NEXT HREF="m_multip.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="m_case_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_multip.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="typecase"><A NAME="ctypecase"><A NAME="etypecase"><I>Macro</I> <B>TYPECASE, CTYPECASE, ETYPECASE</B></A></A></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>typecase</B> <I>keyform {<I>normal-clause</I>}<B>*</B> [<I>otherwise-clause</I>]</I> => <I><I>result</I><B>*</B></I><P>
|
|
|
|
<B>ctypecase</B> <I>keyplace {<I>normal-clause</I>}<B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
|
|
|
<B>etypecase</B> <I>keyform {<I>normal-clause</I>}<B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<PRE>
|
|
normal-clause::= (type form*)
|
|
</PRE>
|
|
<PRE>
|
|
otherwise-clause::= ({otherwise | t} form*)
|
|
</PRE>
|
|
<PRE>
|
|
clause::= normal-clause | otherwise-clause
|
|
</PRE>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>keyform</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated to produce a <I>test-key</I>. <P>
|
|
<I>keyplace</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated initially to produce a <I>test-key</I>. Possibly also used later as a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> if no <I>types</I> match. <P>
|
|
<I>test-key</I>---an object produced by evaluating <I>keyform</I> or <I>keyplace</I>. <P>
|
|
<I>type</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I> in the matching <I>clause</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
These <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> allow the conditional execution of a body of <I>forms</I> in a <I>clause</I> that is selected by matching the <I>test-key</I> on the basis of its <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>. <P>
|
|
The <I>keyform</I> or <I>keyplace</I> is <I>evaluated</I> to produce the <I>test-key</I>. <P>
|
|
Each of the <I>normal-clauses</I> is then considered in turn. If the <I>test-key</I> is of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> given by the <I>clauses</I>'s <I>type</I>, the <I>forms</I> in that <I>clause</I> are <I>evaluated</I> as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>, and the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> it returns are returned as the value of the <A REL=DEFINITION HREF="#typecase"><B>typecase</B></A>, <A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A>, or <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
|
These <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> differ only in their <I>behavior</I> when no <I>normal-clause</I> matches; specifically: <P>
|
|
<P><DL><P>
|
|
<DT><A REL=DEFINITION HREF="#typecase"><B>typecase</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, and there is an <I>otherwise-clause</I>, then that <I>otherwise-clause</I> automatically matches; the <I>forms</I> in that <I>clause</I> are <I>evaluated</I> as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>, and the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> it returns are returned as the value of the <A REL=DEFINITION HREF="#typecase"><B>typecase</B></A>. <P>
|
|
If there is no <I>otherwise-clause</I>, <A REL=DEFINITION HREF="#typecase"><B>typecase</B></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<DT><A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, a <A REL=DEFINITION HREF="26_glo_c.htm#correctable"><I>correctable</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#error"><I>error</I></A> 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. The offending datum is the <I>test-key</I> and the expected type is <A REL=DEFINITION HREF="26_glo_t.htm#type_equivalent"><I>type equivalent</I></A> to <TT>(or </TT><I>type1</I><TT> </TT><I>type2</I><TT> ...)</TT>. The <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> can be used to correct the error. <P>
|
|
If the <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> is invoked, its <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> becomes the new <I>test-key</I>, and is stored in <I>keyplace</I> as if by <TT>(setf </TT><I>keyplace</I><TT> </TT><I>test-key</I><TT>)</TT>. Then <A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A> starts over, considering each <I>clause</I> anew. <P>
|
|
If the <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> is invoked interactively, the user is prompted for a new <I>test-key</I> to use. <P>
|
|
The subforms of <I>keyplace</I> might be evaluated again if none of the cases holds. <P>
|
|
<DT><A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, a <A REL=DEFINITION HREF="26_glo_n.htm#non-correctable"><I>non-correctable</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#error"><I>error</I></A> 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. The offending datum is the <I>test-key</I> and the expected type is <A REL=DEFINITION HREF="26_glo_t.htm#type_equivalent"><I>type equivalent</I></A> to <TT>(or </TT><I>type1</I><TT> </TT><I>type2</I><TT> ...)</TT>. <P>
|
|
Note that in contrast with <A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A>, the caller of <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> may rely on the fact that <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> does not return if a <I>normal-clause</I> does not match. <P>
|
|
<P></DL><P>
|
|
In all three cases, is permissible for more than one <I>clause</I> to specify a matching <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A>, particularly if one is a <A REL=DEFINITION HREF="26_glo_s.htm#subtype"><I>subtype</I></A> of another; the earliest applicable <I>clause</I> is chosen. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
;;; (Note that the parts of this example which use TYPE-OF
|
|
;;; are implementation-dependent.)
|
|
(defun what-is-it (x)
|
|
(format t "~&~S is ~A.~%"
|
|
x (typecase x
|
|
(float "a float")
|
|
(null "a symbol, boolean false, or the empty list")
|
|
(list "a list")
|
|
(t (format nil "a(n) ~(~A~)" (type-of x))))))
|
|
=> WHAT-IS-IT
|
|
(map 'nil #'what-is-it '(nil (a b) 7.0 7 box))
|
|
>> NIL is a symbol, boolean false, or the empty list.
|
|
>> (A B) is a list.
|
|
>> 7.0 is a float.
|
|
>> 7 is a(n) integer.
|
|
>> BOX is a(n) symbol.
|
|
=> NIL
|
|
(setq x 1/3)
|
|
=> 1/3
|
|
(ctypecase x
|
|
(integer (* x 4))
|
|
(symbol (symbol-value x)))
|
|
>> Error: The value of X, 1/3, is neither an integer nor a symbol.
|
|
>> To continue, type :CONTINUE followed by an option number:
|
|
>> 1: Specify a value to use instead.
|
|
>> 2: Return to Lisp Toplevel.
|
|
>> Debug> :CONTINUE 1
|
|
>> Use value: 3.7
|
|
>> Error: The value of X, 3.7, is neither an integer nor a symbol.
|
|
>> To continue, type :CONTINUE followed by an option number:
|
|
>> 1: Specify a value to use instead.
|
|
>> 2: Return to Lisp Toplevel.
|
|
>> Debug> :CONTINUE 1
|
|
>> Use value: 12
|
|
=> 48
|
|
x => 12
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A> and <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A>, since they might signal an error, are potentially affected by existing <I>handlers</I> and <A REL=DEFINITION HREF="v_debug_.htm#STdebug-ioST"><B>*debug-io*</B></A>. <P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A> and <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> signal 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> if no <I>normal-clause</I> matches. <P>
|
|
The <A REL=DEFINITION HREF="26_glo_c.htm#compiler"><I>compiler</I></A> may choose to issue a warning of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_style_.htm#style-warning"><B>style-warning</B></A> if a <I>clause</I> will never be selected because it is completely shadowed by earlier clauses. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_case_.htm#case"><B>case</B></A>, <A REL=DEFINITION HREF="m_cond.htm#cond"><B>cond</B></A>, <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A>, <A REL=CHILD HREF="05_a.htm">Section 5.1 (Generalized Reference)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(typecase test-key
|
|
{(type form*)}*)
|
|
==
|
|
(let ((#1=#:g0001 test-key))
|
|
(cond {((typep #1# 'type) form*)}*))
|
|
</PRE>
|
|
</TT> <P>
|
|
The specific error message used by <A REL=DEFINITION HREF="#etypecase"><B>etypecase</B></A> and <A REL=DEFINITION HREF="#ctypecase"><B>ctypecase</B></A> can vary between implementations. In situations where control of the specific wording of the error message is important, it is better to use <A REL=DEFINITION HREF="#typecase"><B>typecase</B></A> with an <I>otherwise-clause</I> that explicitly signals an error with an appropriate message. <P>
|
|
<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>
|