Created
January 4, 2024 17:44
-
-
Save aarongodin/d419681176f4821d7f647535b8c6bbd9 to your computer and use it in GitHub Desktop.
Onyx - tagged unions undefined behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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