-
- Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
Description
Objects annotated with the {.union.} pragma work just like plain objects on NimVM.
Example
import strutils type NumberArray = object {.union.} a: array[4, uint8] n_uint32: uint32 n_uint16: uint16 proc print(num: NumberArray) = echo num.n_uint32.toHex echo num.n_uint16.toHex echo num.a[0].toHex echo num.a[1].toHex echo num.a[2].toHex echo num.a[3].toHex echo "" echo "Runtime" var num: NumberArray num.n_uint32 = 0xdeadbeef'u32 num.print() num.n_uint16 = 0xbabe'u16 num.print() num.a[0] = 0xa1 num.a[1] = 0xb2 num.a[2] = 0xc3 num.a[3] = 0xd4 num.print() static: echo "Compile time" var num: NumberArray num.n_uint32 = 0xdeadbeef'u32 num.print() num.n_uint16 = 0xbabe'u16 num.print() num.a[0] = 0xa1 num.a[1] = 0xb2 num.a[2] = 0xc3 num.a[3] = 0xd4 num.print()Current Output
Compile time DEADBEEF 0000 00 00 00 00 DEADBEEF BABE 00 00 00 00 DEADBEEF BABE A1 B2 C3 D4 . . . Runtime DEADBEEF BEEF EF BE AD DE DEADBABE BABE BE BA AD DE D4C3B2A1 B2A1 A1 B2 C3 D4 Expected Output
Compile-time output should match the runtime output.
Proposed Solution
Ideally, union support should be added to NimVM. In the meantime, {.union.} should trigger an error on NimVM otherwise this could lead to bugs.
Additional Information
Nim Compiler Version 1.0.6 [MacOSX: amd64] Compiled at 2020-01-23 Copyright (c) 2006-2019 by Andreas Rumpf git hash: 89b39ee8fe271d0e1b75c15a4c6cf82eb9c13aea active boot switches: -d:release