Common Lisp the Language, 2nd Edition


next up previous contents index
Next: Determining the Type Up: Type Specifiers Previous: Defining New Type

4.8. Type Conversion Function

The following function may be used to convert an object to an equivalent object of another type.


[Function]
coerce object result-type

The result-type must be a type specifier; the object is converted to an ``equivalent'' object of the specified type. If the coercion cannot be performed, then an error is signaled. In particular, (coerce x 'nil) always signals an error. If object is already of the specified type, as determined by typep, then it is simply returned. It is not generally possible to convert any object to be of any type whatsoever; only certain conversions are permitted:

change_begin
X3J13 voted in June 1989 (SEQUENCE-TYPE-LENGTH)   to specify that coerce should signal an error if the new sequence type specifies the number of elements and the old sequence has a different length.

X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to specify that if the result-type is string then it is understood to mean (vector character), and simple-string is understood to mean (simple-array character (*)).
change_end

old_change_begin


old_change_end

change_begin
X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate int-char from Common Lisp. Presumably this eliminates the possibility of coercing an integer to a character, although the vote did not address this question directly.
change_end

change_begin
X3J13 voted in June 1988 (FUNCTION-TYPE)   to allow coercion of certain objects to the type function:

change_end

Coercions from floating-point numbers to rationals and from ratios to integers are purposely not provided because of rounding problems. The functions rational, rationalize, floor, ceiling, truncate, and round may be used for such purposes. Similarly, coercions from characters to integers are purposely not provided; char-code or char-int may be used explicitly to perform such conversions.



next up previous contents index
Next: Determining the Type Up: Type Specifiers Previous: Defining New Type


AI.Repository@cs.cmu.edu