r/Compilers • • Oct 17 '26

nullptr, zero initialization, is zero checks design question

Hi,

I'm working on mox (low level + metaprogramming) and currently in mox I have zero initialization for everything. For example:

x: T = 0;
x: []T = 0;
x: [10]T = 0;

Zero initializes the value. If T is struct, all fields gets zeroed, if it is fixed array, all elements are zeroed, if it is slice, it has ptr=0, len=0.

But the problem and question is with pointers. At first I thought adding explicit type for nullable pointers is a good thing. Because then you have explicit and readable contract on function arguments and you possibly could have some optimizations or checks in compiler. Because of this, not nullable pointer could not be initialized with zero (because it is semantically wrong).

But while I was writing in C++ and in mox I found that I never ever used nullable pointers. And not able to zero any pointer type just makes more headache. Having nil like in Odin instead of 0 makes not much sense for me. You just write more letters and think more about some special nil while 0 clearly says that you zero the value.

nullptr problem currently solved by having nullptr function:

fn foo(x: *i32) {}
fn foo(x: i32) {}

foo(nullptr(i32));

And in case we need zeroed value with expression, I have zeroed function:

fn foo(x: Smth) {}

foo(zeroed(Smth));

So the question is, what do you think about having explicit nil for zeroing and nullable pointers? How often do you really use it? Are there any other problems/things that I dont see here?

10 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/whizzter Oct 18 '26 edited Oct 18 '26

Personally I feel that 0-init is a useful middle step when going from very low-level languages to slightly higher level, however much meat in larger programs usually concerns more highlevel concerns, and while having good semantics for controlling how to make fast low-level code, being able to express higher level concerns in the language is better in the long run.

This is why C# is so successful in gamedev, it’s bloaty but a lot of things is possible quickly to prototype, and while you’ll have a time to clear out slow crap when nearing release, it’s got features to make it work well enough without adding an additional scripting language (so while C# isn’t 100%, something lowlevel if you add a scripting runtime isn’t always better).

Make common things easy, if that is 0-init then that’s the right thing, but facilitating immediate correct init helps a lot of logic be simpler (both in C# and C++ I can use non-null pointers(refs by semantic in C++) a large fraction of the time).

As a personal point where I think the is gaps is lower level languages to have facilities for static, stack, pingpong(buffer), etc arrays to be auto-sized/reused by compiler facilities so that you can init values into them, so there never is an ”uninitialized” state, just not existing or fully prepared. (Yes, this is a bit or functional thinking but there’s nothing inherently contradictory to lower level languages if you have in the design, and comptime helps that work better).