Chapter 5

Types and Classes

# Union Types

Union types represent the union of the instances of two or more other types. Union types
are created with the function `type-union`

. They are not
classes.

Union types are useful as slot specializers, and describe the return types of many common
functions. For example, the return type of the collection method on `size`

could
be expressed as `type-union(<integer>, singleton(#f))`

.

define constant <green-thing> = type-union(<frog>, <broccoli>); define constant kermit = make(<frog>); define method red? (x :: <green-thing>) #f end method; red?(kermit) ⇒ #f

The following rules govern `subtype?`

and `instance?`

for union types.

Given

`x`is an object.`s`…_{1}`s`and_{m}`t`…_{1}`t`are nonunion types._{n}- The notation
`type-union*(`

stands for any arrangement of nested calls to`t`…_{1}`t`)_{n}`type-union`

, where none of the arguments is a subtype of any other, and none of the arguments forms an exhaustive partition of any other type.

Then

`type-union(`

is type equivalent to `t_{1}`,

`t`)

_{1}`t`.

_{1}`type-union(`

is type equivalent to
`t_{1}`,

`t`)

_{2}`type-union(``t`_{2}, `t`_{1})

.`type-union(`

is type equivalent to
`t_{1}`,
type-union(

`t`,

_{2}`t`))

_{3}`type-union(type-union(``t`_{1}, `t`_{2}), `t`_{3})

.`type-union(`

is type equivalent to `t_{1}`,

`t`)

_{2}`t`when

_{2}`subtype?(``t`_{1}, `t`_{2})

.`instance?(`

will be true if and only if `x`,
type-union*(`t_{1}`…

`t`))

_{n}`instance?(``x`, `t`)

is true for
some `t`in

`t`…

_{1}`t`.

_{n}`subtype?(type-union*(`

will be true if and only if
`t_{1}`…

`t`),

_{n}`s`)

_{1}`subtype?(``t`, `s`_{1})

is true for
every `t`in

`t`…

_{1}`t`.

_{n}`subtype?(`

will be true if and only if
`s_{1}`,
type-union*(

`t`…

_{1}`t`))

_{n}`subtype?(``s`_{1}, `t`)

is true for
some `t`in

`t`…

_{1}`t`.

_{n}`subtype?(type-union*(`

will be true if and only if every `s_{1}`…

`s`), type-union*(

_{m}`t`…

_{1}`t`))

_{n}`s`in

`s`…

_{1}`s`is a subtype of some

_{m}`t`in

`t`…

_{1}`t`.

_{n}
**Errata:** In the published book,
the comma between the arguments to `subtype?`

is missing.