next up previous contents
Next: 5.2.8 Structure Types Up: 5.2 More About Types Previous: 5.2.6 Function Types

5.2.7 The Values Declaration

 

CMU Common Lisp supports the values declaration as an extension to Common Lisp. The syntax is (values type1 type2tex2html_wrap_inline23388typen). This declaration is semantically equivalent to a the form wrapped around the body of the special form in which the values declaration appears. The advantage of values over  the is purely syntactic--it doesn't introduce more indentation. For example:

(defun foo (x)
  (declare (values single-float))
  (ecase x
    (:this ...)
    (:that ...)
    (:the-other ...)))
is equivalent to:
(defun foo (x)
  (the single-float
       (ecase x
         (:this ...)
         (:that ...)
         (:the-other ...))))
and
(defun floor (number &optional (divisor 1))
  (declare (values integer real))
  ...)
is equivalent to:
(defun floor (number &optional (divisor 1))
  (the (values integer real)
       ...))
In addition to being recognized by lambda (and hence by defun), the values declaration is recognized by all the other special forms with bodies and declarations: let, let*, labels and flet. Macros with declarations usually splice the declarations into one of the above forms, so they will accept this declaration too, but the exact effect of a values declaration will depend on the macro.

If you declare the types of all arguments to a function, and also declare the return value types with values, you have described the type of the function. Python will use this argument and result type information to derive a function type that will then be applied to calls of the function (see section 5.2.6.) This provides a way to declare the types of functions that is much less syntactically awkward than using the ftype declaration with a function type specifier.

Although the values declaration is non-standard, it is relatively harmless to use it in otherwise portable code, since any warning in non-CMU implementations can be suppressed with the standard declaration proclamation.



Raymond Toy
Mon Jul 14 09:11:27 EDT 1997