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.
type
EventType* {.size: sizeof(uint32).} = enum
QuitEvent,
AppTerminating,
AppLowMemory
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.
type
AtomicFlag* {.importc: "atomic_flag", header: "<stdatomic.h>", size: 1.} = object
static:
# if AtomicFlag didn't have the size pragma, this code would result in a compile time error.
echo sizeof(AtomicFlag)
The size pragma accepts only the values 1, 2, 4 or 8.
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .