size pragma

Nim automatically determines the size of an enum. But when wrapping a C enum type, it needs to be of a specific size. The size pragma allows specifying the size of the enum type.

  1. type
  2. EventType* {.size: sizeof(uint32).} = enum
  3. QuitEvent,
  4. AppTerminating,
  5. AppLowMemory
  6. doAssert sizeof(EventType) == sizeof(uint32)

The size pragma can also specify the size of an importc incomplete object type so that one can get the size of it at compile time even if it was declared without fields.

  1. type
  2. AtomicFlag* {.importc: "atomic_flag", header: "<stdatomic.h>", size: 1.} = object
  3. static:
  4. # if AtomicFlag didn't have the size pragma, this code would result in a compile time error.
  5. echo sizeof(AtomicFlag)

The size pragma accepts only the values 1, 2, 4 or 8.