typecase
keyform {!normal-clause}{* [!otherwise-clause]} => {result}{*}
ctypecase
keyplace {!normal-clause}{*} => {result}{*}
etypecase
keyform {!normal-clause}{*} => {result}{*}
normal-clause ::=(type {form}{*)}
otherwise-clause ::=({otherwise | t} {form}{*)}
clause ::=normal-clause | otherwise-clause
keyform---a form; evaluated to produce a test-key.
keyplace---a form; evaluated initially to produce a test-key. Possibly also used later as a place if no types match.
test-key---an object produced by evaluating keyform or keyplace.
type---a type specifier.
forms---an implicit progn.
results---the values returned by the forms in the matching clause.
These macros allow the conditional execution of a body of forms in a clause that is selected by matching the test-key on the basis of its type.
The keyform or keyplace is evaluated to produce the test-key.
Each of the normal-clauses is then considered in turn. If the test-key is of the type given by the clauses's type, the forms in that clause are evaluated as an implicit progn, and the values it returns are returned as the value of the typecase, ctypecase, or etypecase form.
These macros differ only in their behavior when no normal-clause matches; specifically:
In all three cases, is permissible for more than one clause to specify a matching type, particularly if one is a subtype of another; the earliest applicable clause is chosen.
;;; (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
ctypecase and etypecase, since they might signal an error, are potentially affected by existing handlers and *debug-io*.
ctypecase and etypecase signal an error of type type-error if no normal-clause matches.
The compiler may choose to issue a warning of type style-warning if a clause will never be selected because it is completely shadowed by earlier clauses.
@xref{case; ccase; ecase} , section cond [Macro] , @xref{setf; psetf} , section Generalized Reference
(typecase test-key {(type {form}{*})}{*}) == (let ((#1=#:g0001 test-key)) (cond {((typep #1# 'type) {form}{*})}{*}))
The specific error message used by etypecase and ctypecase can vary between implementations. In situations where control of the specific wording of the error message is important, it is better to use typecase with an otherwise-clause that explicitly signals an error with an appropriate message.
Go to the first, previous, next, last section, table of contents.