Thoughts

mental health break ,./'"**^^$_---
The reason comptime is bad for safety checks is that function calls don’t propagate the comptime-known-ness value of their parameters.
So if you have a safety check that you want to run in a function five levels deep, even if the value is comptime known at the top level, you have to make all of the intervening levels either 1) only accept comptime arguments or 2) make them all inline. Both of these are strongly discouraged by the Zig core team. The former because it’s contrary to one of the goals of Zig, that you can write code that runs at comptime or runtime. The second is discouraged because marking functions inline hurts the compilers ability to optimize them. (Inline functions are inlined before optimizations are performed, which means that optimizing the parent function is harder because there’s a larger search space.) You might think this is a flaw in Zig and there should be a way for the compiler to optionally propagate comptime known parameters without requiring them to be comptime, but it’s very difficult to define semantics for how this would work in practice (are you generating two copies of the function or one?) If you want safety checks, you’d be much better off performing them at runtime in debug mode.
Link 12:09 p.m. Nov 09, 2024 UTC-5