mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 07:43:26 +00:00
Document about size pragma (#21794)
* Document about size pragma * Fix typos * Fix manual.md * Update doc/manual.md --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
@@ -7485,6 +7485,37 @@ generates:
|
||||
```
|
||||
|
||||
|
||||
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.
|
||||
|
||||
```Nim
|
||||
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.
|
||||
|
||||
```Nim
|
||||
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.
|
||||
|
||||
|
||||
Align pragma
|
||||
------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user