On Thu, Dec 3, 2026 at 01:51 Chris Hanson <[email protected]>
wrote:

> I’m designing a subsystem (including system calls) and I’d like to use
> “typed enums” in the API rather than #define or anonymous enums and int
> parameters.
>
> For example:
>
>     typedef enum : int {
>             subsys_thing_a = 0,
>             subways_thing_b,
>     } subsys_thing_t;
>
>     int subsys_call(subsys_thing_t thing);
>
> Many years ago at Apple we brought these from C++ to Objective-C and C
> because they can dramatically improve debugging ergonomics, and they’re
> part of C23, but can we actually require C23 for -current?
>
> Experimentally, I’ve verified that they work without complaint or problem
> on virt68k (my preferred machine to use for experiments, for debugging
> ease) but I’d wonder about things like VAX.
>
> In the worst case I can always create a wrapper macro (like we did at
> Apple) so my API can work with compilers that both do and don’t support
> them, but it’d be nice if I didn’t have to.
>
>   -- Chris
>

my approach to this is to work within the compiler limits, tie down the API
and ABI so that future upgrades work fine, and try to work as efficiently
as possible, using 4 functions for everything.

Use an opaque type, one function to allocate, one to free resources. Pass
the address of the handle to the free function, snd set to NULL in there,
to avoid UAF

Two functions that operate on the opaque type, one returning a number, one
returning allocated storage. Caller frees. The action/method to perform is
a constant C string, using lightweight hashing so can switch on string
value. All values in switch are defined up front, so compiler rules out
hash collisions. It's robust in terms of future-proofing, and introspection
can be done on version number, or "supports-operation" type methods.
Standard C99, no compiler/runtime/debugger support necessary. Works with C
and C++ callers

For a worked example, and a topical one?, see pkgsrc/security/{lib,}mldsa,
or go to https://codeberg.org/agc-codeberg/mldsa

Stringswitch utility for generating case values is at
https://codeberg.org/agc-codeberg/stringswitch

>
Not saying your approach is wrong, just that there's more than one way to
address this

>
>

Reply via email to