89 lines
4.9 KiB
HTML
89 lines
4.9 KiB
HTML
![]() |
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
||
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
||
|
<HEAD>
|
||
|
<TITLE>9.3. Type Declaration for Forms</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Type Declaration for Forms">
|
||
|
<meta name="keywords" value="clm">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<b>Common Lisp the Language, 2nd Edition</b>
|
||
|
<BR> <HR><A NAME=tex2html2841 HREF="node107.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2839 HREF="node103.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2835 HREF="node105.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2843 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2844 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html2842 HREF="node107.html"> Symbols</A>
|
||
|
<B>Up:</B> <A NAME=tex2html2840 HREF="node103.html"> Declarations</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html2836 HREF="node105.html"> Declaration Specifiers</A>
|
||
|
<HR> <P>
|
||
|
<H1><A NAME=SECTION001330000000000000000>9.3. Type Declaration for Forms</A></H1>
|
||
|
<P>
|
||
|
Frequently it is useful to declare that the value produced
|
||
|
by the evaluation of some form will be of a particular type.
|
||
|
Using <tt>declare</tt> one can declare the type of the value
|
||
|
held by a bound variable, but there is no easy way to declare
|
||
|
the type of the value of an unnamed form. For this purpose the <tt>the</tt>
|
||
|
special form is defined; <tt>(the <i>type</i> <i>form</i>)</tt> means
|
||
|
that the value of <i>form</i> is declared to be of type <i>type</i>.
|
||
|
<P>
|
||
|
<BR><b>[Special Form]</b><BR>
|
||
|
<tt>the</tt> <tt><i>value-type</i></tt> <tt><i>form</i></tt><P>The <i>form</i> is evaluated; whatever it produces is returned by
|
||
|
the <tt>the</tt> form. In addition, it is an error if what is produced
|
||
|
by the <i>form</i> does not conform to the data type specified by <i>value-type</i>
|
||
|
(which is not evaluated). (A given implementation may or may not
|
||
|
actually check for this error. Implementations are encouraged to make an
|
||
|
explicit error check when running interpretively.) In effect, this
|
||
|
declares that the user undertakes to guarantee that the values of
|
||
|
the form will always be of the specified type.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(the string (copy-seq x)) ;The result will be a string
|
||
|
(the integer (+ x 3)) ;The result of <tt>+</tt> will be an integer
|
||
|
(+ (the integer x) 3) ;The value of <tt>x</tt> will be an integer
|
||
|
(the (complex rational) (* z 3))
|
||
|
(the (unsigned-byte 8) (logand x mask))
|
||
|
</pre><P>
|
||
|
The <tt>values</tt> type specifier may be used to indicate the types
|
||
|
of multiple values:
|
||
|
<P><pre>
|
||
|
(the (values integer integer) (floor x y))
|
||
|
(the (values string t)
|
||
|
(gethash the-key the-string-table))
|
||
|
</pre><P>
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in June 1989 (THE-AMBIGUITY) <A NAME=9713> </A>
|
||
|
to clarify that <i>value-type</i> may be any valid type specifier whatsoever.
|
||
|
The point is that a type specifier need not be one suitable for
|
||
|
discrimination but only for declaration.
|
||
|
<P>
|
||
|
In the case that the <i>form</i> produces exactly one value and <i>value-type</i>
|
||
|
is not a <tt>values</tt> type specifier, one may describe a <tt>the</tt> form
|
||
|
as being entirely equivalent to
|
||
|
<P><pre>
|
||
|
(let ((#1=#:temp <i>form</i>)) (declare (type <i>value-type</i> #1#)) #1#)
|
||
|
</pre><P>
|
||
|
A more elaborate expression could be written to describe the case where
|
||
|
<i>value-type</i> is a <tt>values</tt> type specifier.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<hr>
|
||
|
<b>Compatibility note:</b> This construct is borrowed from the Interlisp DECL
|
||
|
package; Interlisp, however, allows an implicit <tt>progn</tt> after the type
|
||
|
specifier rather than just a single form. The MacLisp <tt>fixnum-identity</tt>
|
||
|
and <tt>flonum-identity</tt> constructs can be expressed as <tt>(the fixnum <i>x</i>)</tt>
|
||
|
and <tt>(the single-float <i>x</i>)</tt>.
|
||
|
<hr>
|
||
|
<P>
|
||
|
|
||
|
<P>
|
||
|
<BR> <HR><A NAME=tex2html2841 HREF="node107.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2839 HREF="node103.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2835 HREF="node105.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2843 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2844 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html2842 HREF="node107.html"> Symbols</A>
|
||
|
<B>Up:</B> <A NAME=tex2html2840 HREF="node103.html"> Declarations</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html2836 HREF="node105.html"> Declaration Specifiers</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|