Skip to content

Instantly share code, notes, and snippets.

@joakin
Created August 7, 2023 22:57
Show Gist options
  • Save joakin/451261ba136fe3146d9ac11d06a638c8 to your computer and use it in GitHub Desktop.
Save joakin/451261ba136fe3146d9ac11d06a638c8 to your computer and use it in GitHub Desktop.
Odin confusion with nil
package main
import "core:fmt"
Test :: struct {
test: int
}
EnumTest :: enum {
Int,
String,
}
UnionTest :: union {
EnumTest,
Test,
}
UnionTestNotNil :: union #no_nil {
Test,
EnumTest,
}
main :: proc() {
a : Test
// Won't compile, nil can't be a Test
// if a == nil {
// fmt.println("Test, it was nil")
// } else {
// fmt.println("Test, it was NOT nil")
// }
fmt.println("%v", a)
//> %v Test{test = 0}
b : EnumTest
if b == nil {
fmt.println("EnumTest, it was nil")
} else {
fmt.println("EnumTest, it was NOT nil")
}
//> EnumTest, it was nil
fmt.println("%v", b)
//> %v Int
c : UnionTest
if c == nil {
fmt.println("UnionTest, it was nil")
} else {
fmt.println("UnionTest, it was NOT nil")
}
//> UnionTest, it was nil
fmt.println("%v", c)
//> %v nil
d : UnionTestNotNil
// Won't compile, nil can't be a UnionTestNotNil
// if d == nil {
// fmt.println("UnionTestNotNil, it was nil")
// } else {
// fmt.println("UnionTestNotNil, it was NOT nil")
// }
fmt.println("%v", d)
//> %v Test{test = 0}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment