# Reading a filled nullable

**URL:** <https://ziggit.dev/t/reading-a-filled-nullable/13037>\
**Category:** Brainstorming\
**Tags:** language\
**Created:** [November 14, 2025, 12:06pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037 "2025-11-14T12:06:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 14, 2025, 12:06pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/1 "2025-11-14T12:06:25Z")

</div>

A snippet from my chess program:  
I have a nullable eval variable which is only filled if not `is_check`.

We know `eval` is filled when `is_check` is false.  
Is using `eval.?` the best way to code it?

Does it have any performance implications to read `eval.?` instead of a raw int (in release mode)?

```zig
const eval: ?i32 = if (is_check) null else evaluate(); // correct edited example

// const eval: i32 = if (!is_check) null else evaluate(); // wrong first example

// ...
// lots of code in between.
// ...
if (!is_check and some_other_conditions) {
    if (eval.? == 42) { do something }
}

if (!is_check and more_conditions) {
    if (eval.? == 43) { do something }
}

```

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [November 14, 2025, 12:33pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/2 "2025-11-14T12:33:03Z")

</div>

> [@ericlang](#):
>
> Is using `eval.?` the best way to code it?

Yes, that’s the intended use-case for `foo.?`.

From the [language reference](https://ziglang.org/documentation/master/#Operators):

```zig
Equivalent to:

a orelse unreachable

```

(so it allows the compiler to optimize based on your guarantee that the value will never be null at that point)

---

<div class="post-metadata">

**Author:** ![Justus2308](https://ziggit.dev/user_avatar/ziggit.dev/justus2308/32/5305_2.png) [@Justus2308](https://ziggit.dev/u/Justus2308)\
**Post date:** [November 14, 2025, 1:48pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/3 "2025-11-14T13:48:19Z")

</div>

I’d also consider this:

```zig
const eval: i32 = if (!is_check) undefined else evaluate();
// ...
// lots of code in between.
// ...
if (!is_check and some_other_conditions) {
    if (eval == 42) { do something }
}

if (!is_check and more_conditions) {
    if (eval == 43) { do something }
}

```

That way you don’t have to bother with unwrapping `eval` everywhere you need it ~~and still get a runtime crash in safe modes if you accidentaly try to access `eval` anywhere you shouldn’t because it’ll be `undefined`.~~

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 14, 2025, 2:51pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/4 "2025-11-14T14:51:50Z")

</div>

True for a local as in the example.  
However these evals are stored in a big stack of nodes and reused so I stick with nullable.  
(To explain: when going deeper into the search-tree i compare current evals with previous evals (if available) to check improvement)

---

<div class="post-metadata">

**Author:** ![affine-root-system](https://ziggit.dev/user_avatar/ziggit.dev/affine-root-system/32/6050_2.png) [@affine-root-system](https://ziggit.dev/u/affine-root-system)\
**Post date:** [November 14, 2025, 4:22pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/5 "2025-11-14T16:22:02Z")

</div>

Not to nitpick, but is your first line backwards? Feels like you want to set `eval` to `null` when `is_check`.

---

<div class="post-metadata">

**Author:** ![truly-not-taken](https://ziggit.dev/user_avatar/ziggit.dev/truly-not-taken/32/6205_2.png) [@truly-not-taken](https://ziggit.dev/u/truly-not-taken)\
**Post date:** [November 15, 2025, 5:18am UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/6 "2025-11-15T05:18:26Z")

</div>

> [@Justus2308](#):
>
> `const eval: i32`

Did you mean `?i32`?

---

<div class="post-metadata">

**Author:** ![Justus2308](https://ziggit.dev/user_avatar/ziggit.dev/justus2308/32/5305_2.png) [@Justus2308](https://ziggit.dev/u/Justus2308)\
**Post date:** [November 15, 2025, 12:40pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/7 "2025-11-15T12:40:49Z")

</div>

No it’d have to be `?i32` in the original snippet but `i32` can be `undefined` (just like any other value)

---

<div class="post-metadata">

**Author:** ![ericlang](https://ziggit.dev/user_avatar/ziggit.dev/ericlang/32/8515_2.png) [@ericlang](https://ziggit.dev/u/ericlang)\
**Post date:** [November 15, 2025, 1:56pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/8 "2025-11-15T13:56:06Z")

</div>

YES. I will change it

---

<div class="post-metadata">

**Author:** ![truly-not-taken](https://ziggit.dev/user_avatar/ziggit.dev/truly-not-taken/32/6205_2.png) [@truly-not-taken](https://ziggit.dev/u/truly-not-taken)\
**Post date:** [November 15, 2025, 5:08pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/9 "2025-11-15T17:08:11Z")

</div>

> [@Justus2308](#):
>
> still get a runtime crash in safe modes if you accidentaly try to access `eval` anywhere you shouldn’t because it’ll be `undefined`.

I am failing to crash my program. What am I doing wrong?

```zig
var runtime_known = false;
runtime_known = !runtime_known;
const foo: i32 = if (runtime_known) undefined else 69;
if (foo == 42) {
    std.debug.print("==42: {}\n", .{ foo });
}
if (foo != 43) {
    std.debug.print("!=43: {}\n", .{ foo });
}

```

Output:

```zig
!=43: 1

```

It does not crash. It just uses garbage value.

P.S. I wonder why the garbage is not 0xAA pattern.

---

<div class="post-metadata">

**Author:** ![Justus2308](https://ziggit.dev/user_avatar/ziggit.dev/justus2308/32/5305_2.png) [@Justus2308](https://ziggit.dev/u/Justus2308)\
**Post date:** [November 15, 2025, 5:37pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/10 "2025-11-15T17:37:14Z")

</div>

Yeah sorry you’re right. That would be [#211](https://github.com/ziglang/zig/issues/211).  
I also find it weird that the value is not `0xAA` though, it’s `0` for me. This:

```zig
    var foo: i32 = undefined;
    if (!runtime_known) foo = 69;

```

does set `foo` to `0xAA` so it might be a bug that your version doesn’t.

---

<div class="post-metadata">

**Author:** ![truly-not-taken](https://ziggit.dev/user_avatar/ziggit.dev/truly-not-taken/32/6205_2.png) [@truly-not-taken](https://ziggit.dev/u/truly-not-taken)\
**Post date:** [November 16, 2025, 12:30pm UTC](https://ziggit.dev/t/reading-a-filled-nullable/13037/11 "2025-11-16T12:30:14Z")

</div>

> [@Justus2308](#):
>
> it might be a bug

Confirmed

> <https://github.com/ziglang/zig/issues/25942>
>
> \### Zig Version
> 
> 0.15.2
> 
> \### Steps to Reproduce and Observed Behavior
> 
> An \`undef…ined\` in a mildly complex expression is not set to a debug value of 0xAA pattern.
> 
> \`\`\`zig
> test "undefined is 0xAA in Debug mode" {
> const baz: u32 = undefined;
> try std.testing.expectEqual(0xAAAAAAAA, baz); // OK
> 
> const bar: u32 = if (true) undefined else 69; // known at comptime
> try std.testing.expectEqual(0xAAAAAAAA, bar); // OK
> 
> var runtime\_known = false;
> runtime\_known = !runtime\_known;
> const foo: u32 = if (runtime\_known) undefined else 42;
> try std.testing.expectEqual(0xAAAAAAAA, foo); // FAILS
> }
> \`\`\`
> 
> On my linux-x86\_64 machine the actual value of \`foo\` is sometimes 0x00000001 and sometimes some other garbage.
> 
> \[Documentation\](https://ziglang.org/documentation/0.15.2/#undefined) implies that \`undefined\` bytes are set to 0xAA.
> 
> Relevant forum post: https://ziggit.dev/t/reading-a-filled-nullable/13037/9
> 
> 
> \### Expected Behavior
> 
> \`undefined\` is 0xAA in safety-checked modes even if it comes from a complex expression.
> 
> If this actually works as intended I expect the documentation to explain it.
> 
> I understand that accessing \`undefined\` is Illegal anyway. My concern is usefulness of 0xAA to detect \`undefined\` when debugging.
