define-enumeration(3scm) Scheme Programmer's Manual define-enumeration(3scm)

define-enumeration - define an enumeration type

(import (rnrs))                     ;R6RS
(import (rnrs enums))               ;R6RS

(define-enumeration type-name
  (symbol ...)
  constructor-syntax)

The define-enumeration form defines an enumeration type and provides two macros for constructing its members and sets of its members.

A define-enumeration form is a definition and can appear anywhere any other definitions can appear.

The type-name argument is an identifier that is bound as a syntactic keyword (see below). The symbol arguments are the symbols that comprise the universe of the enumeration. The constructor-syntax argument is an identifier that is bound to a macro (see below).

Type name macro
(type-name symbol) is equivalent to (quote symbol) except that it verifies at macro-expansion time that the name of symbol is in the universe associated with type-name. Otherwise it is a syntax violation.
Constructor macro
(constructor-syntax symbol ...) checks at macro-expansion time whether every symbol is in the universe associated with type-name. It is a syntax violation if one or more is not. Duplicates are allowed. Otherwise (constructor-syntax symbol ...) is equivalent to ((enum-set-constructor (constructor-syntax)) '(symbol ...)).

In the (type-name symbol) and (constructor-syntax symbol ...) forms, only the names of the symbols are significant.

This form cannot appear in a context where it returns a value.

(define-enumeration color
  (black white purple maroon)
  color-set)
(color black)                    =>  black
(color purpel)                   =>  &syntax exception
(enum-set->list (color-set))     =>  ()
(enum-set->list
 (color-set maroon white))       =>  (white maroon)

Enumerations are used in code that would perhaps traditionally represent flags using bits in integers or lists of symbols.

Enumerations were added to support flags in the R6RS I/O system.

This syntax can raise exceptions with the following condition types:
&syntax (R6RS)
Invalid arguments was passed.

make-enumeration(3scm)

R6RS, SRFI-209

First appeared in R6RS.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

2022-03-26