On this page:
enum
enum->string
enum->pretty-string
enum-value?
enum-prettify
define-enum
Version: 4.0.2.5

16 Enumerations

 (require (planet untyped/unlib/enum))

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.

(enum->string enum)  string?

  enum : enum?

Returns a string representation of (enum-values enum), useful for including in debugging output.

Examples:

  > (define-enum vehicle (car boat plane))

  define-enum vehicle: binding discrepancy: car is predefined

  and not eq? to car

  > (enum->string vehicle)

  "boat, plane"

(enum->pretty-string enum)  string?

  enum : enum?

Returns a string representation of (enum-pretty-values enum), useful for describing the possible values to a user.

Examples:

  > (define-enum vehicle (car boat plane))

  define-enum vehicle: binding discrepancy: car is predefined

  and not eq? to car

  > (enum->pretty-string vehicle)

  "boat, plane"

(enum-value? enum value)  boolean?

  enum : enum?

  value : any

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

Examples:

  > (define-enum vehicle (car boat plane))

  define-enum vehicle: binding discrepancy: car is predefined

  and not eq? to car

  > (enum-value? vehicle 'car)

  #f

  > (enum-value? vehicle 'apple)

  #f

(enum-prettify enum value [default])  string?

  enum : enum?

  value : symbol?

  

default

 

:

 

(U string? (-> string?))

 

 

 

=

 

(cut raise-exn exn:fail:contract ...)

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

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

 

value-clause

 

=

 

value-id

 

 

|

 

[value-id pretty-expr]

 

 

|

 

[value-id value-expr pretty-expr]

 

 

 

 

 

value-expr

 

=

 

(U boolean? symbol? integer?)

 

 

 

 

 

pretty-expr

 

=

 

string?

 

 

 

 

 

keyword-arg

 

=

 

#:plural id

Binds the following identifiers:

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

Examples:

  > (define-enum vehicle (car boat plane))

  define-enum vehicle: binding discrepancy: car is predefined

  and not eq? to car

  > car

  #<procedure:car>

  > boat

  boat

  > plane

  plane

  > (vehicle? car)

  reference to undefined identifier: vehicle?

  > (vehicle? 'apple)

  reference to undefined identifier: vehicle?

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

Examples:

  > (define-enum vehicle (car boat plane) #:prefix vehicle-)

  > vehicle-car

  car

  > vehicle-boat

  boat

  > vehicle-plane

  plane