Re: Safe enumerations for codesets
Marc Nieper-WiÃkirchen 14 Dec 2022 08:16 UTC
The disadvantage of this approach is that you bind many identifiers in
your namespace.
For a better approach (which should be promoted instead), see
`define-enumeration' in R6RS and SRFI 209.
Marc
Am Di., 13. Dez. 2022 um 13:09 Uhr schrieb Lassi Kortela <xxxxxx@lassi.io>:
>
> 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.
>