Can someone please explain to me what undefine is and how is it different from null? Or is it like undefined in JS?
’var b: [16]u8 = undefined;’
Source:
std.fmt provides
1 messages · Page 1 of 1 (latest)
Can someone please explain to me what undefine is and how is it different from null? Or is it like undefined in JS?
’var b: [16]u8 = undefined;’
Source:
std.fmt provides
Whn you assign null in JavaScript, you know it must be null. undefined in Zig is actually undefined: data that could be any possible value assignable to that type. For example,
const a: u32 = undefined;
a in this example could be any value assignable to a u32.
When you assign [16]u8 to undefined, you will get an array of 16 u8s with undefined values.
You should only use undefined in situations where you know that you will not ever read the value when it is undefined.
So so I need to check if a is undefined in if-statements? If so; how do I best check this? 🤔
Huh? Is there a null in Zig? 🤔
yes, but it must be explicitly allowed to be null in the type
e.g. u32 can only be u32 values, but ?u32 can be all u32 values or null
Ah ok. Thank you so much for letting me know. 🙏
I just discovered Zig so I am very new to it. Reading through the docs trying to understand the basics. ☺️
Good luck 🙂
Thank you @placid ginkgo ! 🙏
@grizzled ibex keep in mind that undefined doesn't really exist as a value in runtime. it's a way to tell the compiler to leave the value uninitialized in memory. var foo: u32 = undefined; doesn't assign anything at runtime, it only allocates the space for the variable and doesn't touch its contents
null is a real value that will be assigned at runtime, and so you can check for it