8 Enumerations (revised)
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.
(struct enum (name values pretty-values)) |
name : symbol? |
values : (listof (U boolean? symbol? integer?)) |
pretty-values : (listof string?) |
An enumeration. For each symbol in values there is a human-readable string equivalent in pretty-values.
(define-enum enum-id (value-clause ...) keyword-arg ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Binds enum-id to an enumeration macro that can be used:
in argument position to refer to an enum struct;
in procedure call position to retrieve a value from the enumeration.
Examples: | ||||
| ||||
> (options a) | ||||
option1 | ||||
> (options b) | ||||
"second option" | ||||
> (options c) | ||||
c | ||||
| ||||
("first option" "b" "c") |
The identifier _ can be used as a value-expr as a shorthand for 'value-id. This is useful when writing value clauses where the value-id and the pretty-expr are the most important parts.
Examples: | ||||
| ||||
> (options a) | ||||
a | ||||
> (options b) | ||||
b | ||||
> (options c) | ||||
c | ||||
| ||||
("first option" "second option" "third option") |
Returns a string representation of (enum-values enum), useful for including in debugging output. separator is used to separate the enum values in the return value.
Examples: |
> (define-enum vehicle (car boat plane)) |
> (enum->string vehicle) |
"car, boat, plane" |
Returns a string representation of (enum-pretty-values enum), useful for describing the possible values to a user. separator is used to separate the enum values in the return value.
Examples: |
> (define-enum vehicle (car boat plane)) |
> (enum->pretty-string vehicle) |
"car, boat, plane" |
(enum-value? enum value) → boolean? |
enum : enum? |
value : any |
Returns #t if value is a member of (enum-values enum).
Returns a contract that accepts values from (enum-values enum).
Examples: | ||
> (define-enum vehicle (car boat plane)) | ||
| ||
(car boat plane) |
(for/list ([val (in-enum vehicle car plane)])
val)]
Examples: | ||
> (define-enum vehicle (car boat plane)) | ||
| ||
("car" "boat" "plane") |
(for/list ([val (in-enum vehicle car plane)])
val)]
Examples: |
> (define-enum vehicle (car boat plane)) |
> (enum-value? vehicle 'car) |
#t |
> (enum-value? vehicle 'apple) |
#f |
(enum-prettify enum value [default]) → string? | ||||||||||||
enum : enum? | ||||||||||||
value : symbol? | ||||||||||||
|
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.
(enum-case enum value clause ...) | ||||||||||
|