Safe enumerations for codesets
Lassi Kortela 13 Dec 2022 12:09 UTC
Here's a simple way to roll your own enums:
(define-syntax define-enum
(syntax-rules ()
((define-enum enum symbol ...)
(begin (define symbol 'symbol) ...
(define enum '(symbol ...))))))
(define-enum errno
EPERM
ENOENT
ESRCH
EINTR
EIO
ENXIO
E2BIG
ENOEXEC
EBADF
ECHILD
EDEADLK
ENOMEM
EACCES
EFAULT)
Now typos such as (codeset-message 'errno ECHILE) will be caught.
A safer version such as (codeset-message errno (errno ECHILE)) can
probably be done with syntax-case.
As discussed in an earlier email on this list, it's not appropriate to
define standard enums since the full set of values seen in the wild
cannot be standardized. Each application or library wishing to use safe
enums should list the codes that it needs.