define-enumeration(7scm)
π define-enumeration(7scm)
okay so this is a page from the Scheme Programmer's Manual, and we're about to make enums make sense. stay with me bestie, it's actually so clean β¨
πΈ NAME
define-enumeration β define an enumeration type. that's it. that's the whole vibe.
π LIBRARY
both of these give you the goods:
(import (rnrs)) ;R6RS
(import (rnrs enums)) ;R6RS
π SYNOPSIS
(define-enumeration type-name
(symbol ...)
constructor-syntax)
β¨ DESCRIPTION
okay so define-enumeration is the form that defines an enumeration type, and the way it's giving is generous: it hands you two macros, one for building members and one for building sets of members. two for one, we love a bundle π
define-enumeration is a definition, so it can show up literally anywhere any other definition can show up. no special seating required.
so the arguments:
type-nameβ an identifier that gets bound as a syntactic keyword (peep below π).- the
symbolarguments β these are the symbols that make up the universe of the enumeration. the whole squad. nobody outside the universe gets in. constructor-syntaxβ an identifier bound to a macro (also below π).
π¦ Type name macro
(type-name symbol) is the same energy as (quote symbol), except β and this is the important part β it verifies at macro-expansion time that the name of symbol is actually in the universe associated with type-name. if it's not? syntax violation, bestie. it does not let it slide. π€
π§ Constructor macro
(constructor-syntax symbol ...) checks at macro-expansion time whether every single symbol is in the universe associated with type-name. if one or more of them isn't, it's a syntax violation. periodt.
duplicates are allowed though β no cap, duplicate away π«Ά.
otherwise, (constructor-syntax symbol ...) is equivalent to:
((enum-set-constructor (constructor-syntax)) '(symbol ...))
and one lil detail that matters: in the (type-name symbol) and (constructor-syntax symbol ...) forms, only the names of the symbols are significant. that's the whole check.
π RETURN VALUES
nope! this form can't appear in a context where it returns a value. it's a definition, not a little value-generator. know the difference π
π₯ EXAMPLES
here's the iconic one:
(define-enumeration color
(black white purple maroon)
color-set)
(color black) => black
(color purpel) => &syntax exception
(yes purpel β that's the point, it's not in the universe, so it gets got π)
(enum-set->list (color-set)) => ()
(enum-set->list
(color-set maroon white)) => (white maroon)
see how the empty set is just () and the set comes back sorted-ish by universe order? slay.
π APPLICATION USAGE
enumerations are for code that would perhaps traditionally represent flags using bits in integers, or lists of symbols. you know the type. we're upgrading. π
π RATIONALE
real talk: enumerations were added to support flags in the R6RS I/O system. that's the origin story. main character energy from day one.
π ERRORS
this syntax can raise exceptions with the following condition types:
&syntax(R6RS) β invalid arguments was passed. (their words, not mine, but you get it β bad args, syntax exception, done.)
π«Ά SEE ALSO
β STANDARDS
- R6RS
- SRFI-209
πΈ HISTORY
first appeared in R6RS. a classic.
π AUTHORS
this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more info lives at https://github.com/schemedoc/manpages/.
and that's the whole page, bestie π this drop is from the Scheme Programmer's Manual, define-enumeration(7scm), dated 2022-03-26 β go forth and enumerate, iykyk π
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms