6Enumerations

Utilities for defining simple enumerations of booleans, symbols and integers. These are useful wherever you would normally use a small collection of literals to represent possible values of a variable, and test for value equality with eq?. The define-enum form binds the literals to Scheme identifiers so the compiler catches typos that might otherwise take a long time to debug.

(structenum(namevaluespretty-values))

name:symbol?

values:(listof(Uboolean?symbol?integer?))

pretty-values:(listofstring?)

An enumeration. For each symbol in values there is a human-readable string equivalent in pretty-values.

(enum->stringenum[separator])→string?

enum:enum?

separator:string?=", "

Returns a string representation of (enum-valuesenum), useful for including in debugging output. separator is used to separate the enum values in the return value.

Examples:

> (define-enumvehicle(carboatplane))

> (enum->stringvehicle)

"car, boat, plane"

(enum->pretty-stringenum[separator])→string?

enum:enum?

separator:string?=", "

Returns a string representation of (enum-pretty-valuesenum), useful for describing the possible values to a user. separator is used to separate the enum values in the return value.

Examples:

> (define-enumvehicle(carboatplane))

> (enum->pretty-stringvehicle)

"car, boat, plane"

(enum-value?enumvalue)→boolean?

enum:enum?

value:any

Returns #t if value is a member of (enum-valuesenum).

Examples:

> (define-enumvehicle(carboatplane))

> (enum-value?vehicle'car)

#t

> (enum-value?vehicle'apple)

#f

(enum-prettifyenumvalue[default])→string?

enum:enum?

value:symbol?

default

:

(Ustring?(->string?))

=

(cutraise-exnexn:fail:contract...)

Returns the pretty equivalent of value. If value is not found in enum, default is used instead:

if default is a procedure, it is called to determine the return value;

if default is not a procedure, it is returned.

(define-enumenum-id(value-clause...)keyword-arg...)

value-clause

=

value-id

|

[value-idpretty-expr]

|

[value-idvalue-exprpretty-expr]

value-expr

=

(Uboolean?symbol?integer?)

pretty-expr

=

string?

keyword-arg

=

#:pluralid

Binds the following identifiers:

enum-id: an enumeration struct;

value-id (one binding per value): the values of the enumeration, each a symbol;

enum-id?: a predicate that recognises the values;

enum-id-out: a provide form that provides all of the above.

If value-expr and pretty-expr are unspecified for a value, they are created from value-id.

Examples:

> (define-enumvehicle(carboatplane))

> car

car

> boat

boat

> plane

plane

> (vehicle?car)

#t

> (vehicle?'apple)

#f

The optional #:prefix argument affects the names of the value identifiers (car, boat and so on) bound by the macro: