Unions

Just like structs, unions support embedding.

  1. struct Rgba32_Component {
  2. r byte
  3. g byte
  4. b byte
  5. a byte
  6. }
  7. union Rgba32 {
  8. Rgba32_Component
  9. value u32
  10. }
  11. clr1 := Rgba32{
  12. value: 0x008811FF
  13. }
  14. clr2 := Rgba32{
  15. Rgba32_Component: Rgba32_Component{
  16. a: 128
  17. }
  18. }
  19. sz := sizeof(Rgba32)
  20. unsafe {
  21. println('Size: ${sz}B,clr1.b: $clr1.b,clr2.b: $clr2.b')
  22. }

Output: Size: 4B, clr1.b: 136, clr2.b: 0

Union member access must be performed in an unsafe block.

Note that the embedded struct arguments are not necessarily stored in the order listed.