c2nim icon indicating copy to clipboard operation
c2nim copied to clipboard

C `enum` should not be translated to Nim enum

Open arnetheduck opened this issue 2 years ago • 0 comments

These have different semantics - nim enum are in particular a lot more strict than those of C - specially when they have holes or are used as flags, ie

typedef enum {a = 0x01, b = 0x02, c = a || b} flag;

void f() {
  flag v = flag(42);
};

The better way to map enum to nim is via constants and type EnumType = cint - this can be done with various amounts of machinery around it (distinct, overloaded and etc) - this raises awareness in code that uses the mapped API that they need to be careful around conversions to/from the enum and other pitfalls.

arnetheduck avatar Sep 12 '23 17:09 arnetheduck