4 ms·
> You can explicitly choose to leave it undefined if it will get set later, but zig will error if you try to use it. That doesn't seem to be true. The example
by NieDzejkob 6y ago
> You can explicitly choose to leave it undefined if it will get set later, but zig will error if you try to use it.
That doesn't seem to be true. The example below this sentence errors because the function foo being used doesn't exist, not because the variable is undefined. A proper example [1] shows that Zig will happily use uninitialized memory here:
x: -1431655766
[1]: https://godbolt.org/z/36j4ab https://godbolt.org/z/36j4ab
- dilap 6y agoYes, you're right. From the manual: > [Once a variable is set to undefined], it is no longer possible to detect that the value is undefined. undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used." > In Debug mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger. https://ziglang.org/documentation/master/#undefined https://ziglang.org/documentation/master/#undefined