Variant Types
The module srfi-99-variants exports convenience syntax to create variant types and to destructure their instances. A variant type is a record type with a set of sealed subtypes.
Defining Variant Types
A variant type can be defined using the syntax
(define-variant-type [rtd | (rtd option ...)] predicate (variant field ...) ...)
The syntax elements rtd, option and predicate are used just like the corresponding elements of the define-record-type syntax. The new record type has no fields and no direct constructor.
Each variant identifier is bound to the constructor procedure of a sealed subtype of rtd. The record type descriptor of the subtype is attached as procedure data to the constructor.
The variant subtypes are opaque if and only if the base type is opaque.
Single variants can also be defined separately using the macro define-variant-constructor.
Matching variant types
Given an instance v of a variant type rtd you can use the following syntax to match on the different possible variants of the instance:
(variant-case rtd v ((variant field ...) body ...) ... [(else body ...)])
The form first ensures that v is indeed an instance of rtd, raising a type error otherwise.
Each variant designates a variant constructor, each field is an identifier to be bound to the corresponding, identically named field of the variant instance. If any of the variant clauses matches the type of v, the corresponding body is evaluated and the result is returned from the variant-case form.
If none of the variant clauses match the type of v, the else clause is executed instead. If no else clause is present either, an error is raised indicating that no matching variant was found.