Skip to content

Instantly share code, notes, and snippets.

@aarongodin
Created January 4, 2024 17:44
Show Gist options
  • Save aarongodin/d419681176f4821d7f647535b8c6bbd9 to your computer and use it in GitHub Desktop.
Save aarongodin/d419681176f4821d7f647535b8c6bbd9 to your computer and use it in GitHub Desktop.
Onyx - tagged unions undefined behavior
use core { println }
Item :: struct {
value: str;
}
Other :: struct {
value: i32;
}
Container :: union {
Item: Item;
Other: Other;
}
createItem :: (v: str) -> &Container {
i := Item.{value = v };
c := Container.{Item = i};
return &c;
}
createOther :: (v: i32) -> &Container {
o := Other.{ value = v };
c := Container.{Other = o};
return &c;
}
main :: () {
// Creating two containers that are owned by the createOther proc
// results in each being an "unknown_variant" value
//
// Try running this example with lines relating to `c2` commented.
c1 := createItem("test");
c2 := createOther(123);
println(*c1);
println(*c2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment