1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node53.html
2023-10-25 11:23:21 +02:00

127 lines
6.4 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>4.9. Determining the Type of an Object</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Determining the Type of an Object">
<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=tex2html2163 HREF="node54.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2161 HREF="node44.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2155 HREF="node52.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2165 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2166 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2164 HREF="node54.html"> Type Upgrading</A>
<B>Up:</B> <A NAME=tex2html2162 HREF="node44.html"> Type Specifiers</A>
<B> Previous:</B> <A NAME=tex2html2156 HREF="node52.html"> Type Conversion Function</A>
<HR> <P>
<H1><A NAME=SECTION00890000000000000000>4.9. Determining the Type of an Object</A></H1>
<P>
The following function may be used to obtain a type specifier
describing the type of a given object.
<P>
<BR><b>[Function]</b><BR>
<tt>type-of</tt> <tt><i>object</I></tt><P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
<tt>(type-of <i>object</i>)</tt> returns an implementation-dependent result:
some <i>type</i> of which the <i>object</i> is a member. Implementors
are encouraged to arrange for
<tt>type-of</tt> to return the most specific type that can be
conveniently computed and is likely to be useful to the user.
If the argument is a user-defined named
structure created by <tt>defstruct</tt>, then <tt>type-of</tt> will return the type name
of that structure.
Because the result is implementation-dependent, it is usually better
to use <tt>type-of</tt> primarily for debugging purposes;
however, in a few situations portable code requires the use of
<tt>type-of</tt>, such as when the result is to be given to the
<tt>coerce</tt> or <tt>map</tt> function.
On the other hand, often the <tt>typep</tt> function
or the <tt>typecase</tt> construct
is more appropriate than <tt>type-of</tt>.
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<hr>
<b>Compatibility note:</b> In MacLisp the function <tt>type-of</tt> is called <tt>typep</tt>,
and anomalously so, for it is not a predicate.
<hr>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
Many have observed (and rightly so) that this specification is totally wimpy
and therefore nearly useless. X3J13 voted in June 1989
(TYPE-OF-UNDERCONSTRAINED) <A NAME=2772>&#160;</A>
to place the following constraints on <tt>type-of</tt>:
<UL><LI>
Let <i>x</i> be an object such that <tt>(typep <i>x</i> <i>type</i>)</tt>
is true and <i>type</i> is one of the following:
<P>
<pre>
array float package sequence
bit-vector function pathname short-float
character hash-table random-state single-float
complex integer ratio stream
condition long-float rational string
cons null readtable symbol
double-float number restart vector
</pre>
<P>
Then
<tt>(subtypep (type-of <i>x</i>) <i>type</i>))</tt>
must return the values <tt>t</tt> and <tt>t</tt>; that is, <tt>type-of</tt> applied
to <i>x</i> must return either <i>type</i> itself or a subtype of <i>type</i>
that <tt>subtypep</tt> can recognize in that implementation.
<P>
<LI>
For any object <i>x</i>, <tt>(subtypep (type-of <i>x</i>) (class-of <i>x</i>))</tt>
must produce the values <tt>t</tt> and <tt>t</tt>.
<P>
<LI>
For every object <i>x</i>, <tt>(typep <i>x</i> (type-of <i>x</i>))</tt>
must be true. (This implies that <tt>type-of</tt> can never return <tt>nil</tt>,
for no object is of type <tt>nil</tt>.)
<P>
<LI>
<tt>type-of</tt> never returns <tt>t</tt> and never uses
a <tt>satisfies</tt>, <tt>and</tt>, <tt>or</tt>, <tt>not</tt>,
or <tt>values</tt> type specifier in its result.
<P>
<LI>
For objects of CLOS metaclass <tt>structure-class</tt> or of <tt>standard-class</tt>,
<tt>type-of</tt> returns the proper name of the class returned by <tt>class-of</tt>
if it has a proper name, and otherwise returns the class itself.
In particular,
for any object created by a <tt>defstruct</tt> constructor function,
where the <tt>defstruct</tt> had the name <i>name</i> and no <tt>:type</tt> option,
<tt>type-of</tt> will return <i>name</i>.
</UL>
<P>
As an example, <tt>(type-of &quot;acetylcholinesterase&quot;)</tt>
may return <tt>string</tt> or <tt>simple-string</tt> or <tt>(simple-string 20)</tt>,
but not <tt>array</tt> or <tt>simple-vector</tt>.
As another example, it is permitted for
<tt>(type-of 1729)</tt> to return
<tt>integer</tt> or <tt>fixnum</tt> (if it is indeed a fixnum) or
<tt>(signed-byte 16)</tt> or <tt>(integer 1729 1729)</tt> or <tt>(integer 1685 1750)</tt>
or even <tt>(mod 1730)</tt>, but not <tt>rational</tt> or <tt>number</tt>, because
<P><pre>
(typep (+ (expt 9 3) (expt 10 3)) 'integer)
</pre><P>
is true, <tt>integer</tt> is in the list of types mentioned above, and
<P><pre>
(subtypep (type-of (+ (expt 1 3) (expt 12 3))) 'integer)
</pre><P>
would be false if <tt>type-of</tt> were to return <tt>rational</tt> or <tt>number</tt>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html2163 HREF="node54.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2161 HREF="node44.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2155 HREF="node52.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2165 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2166 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2164 HREF="node54.html"> Type Upgrading</A>
<B>Up:</B> <A NAME=tex2html2162 HREF="node44.html"> Type Specifiers</A>
<B> Previous:</B> <A NAME=tex2html2156 HREF="node52.html"> Type Conversion Function</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>