next up previous contents
Next: 5.2.5 The Empty Type Up: 5.2 More About Types Previous: 5.2.3 Member Types

5.2.4 Union Types

   

The  or (union) type specifier is understood, and is meaningfully applied in many contexts. The use of or allows assertions to be made about types in dynamically typed programs. For example:

(defstruct box
  (next nil :type (or box null))
  (top :removed :type (or box-top (member :removed))))
The type assertion on the top slot ensures that an error will be signalled when there is an attempt to store an illegal value (such as :rmoved.) Although somewhat weak, these union type assertions provide a useful input into type inference, allowing the cost of type checking to be reduced. For example, this loop is safely compiled with no type checks:
(defun find-box-with-top (box)
  (declare (type (or box null) box))
  (do ((current box (box-next current)))
      ((null current))
    (unless (eq (box-top current) :removed)
      (return current))))

Union types are also useful in type inference for representing types that are partially constrained. For example, the result of this expression:

(if foo
    (logior x y)
    (list x y))
can be expressed as (or integer cons).



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