Whenever I’m prototyping or experimenting, which is often, I run into things like this. Let’s say I’m experimenting with Number and the get function.
I’m not sure if this design is going to work, so I don’t want to write unit tests that 1. I’ll be changing frequently because I’m tweaking the design still or 2. I might completely throw away if this design doesn’t pan out.
Anyway, I have this code.
const std = @import("std");
const Number = enum {
one,
two,
};
fn get(n: Number) i32 {
return switch (n) {
.one => "1",
.two => "2",
.three => "3",
};
}
pub fn main(init: std.process.Init) !void {
_ = init;
}
$ zig build --watch
Build Summary: 3/3 steps succeeded
install success
watching 79 directories, 0 processes
Great! Zig was able to compile the code, so I guess that means I got the syntax and types correct.
Wait, wait. I remember something about Zig not building stuff if it’s not referenced or something… let me reference get from main. I usually have to do something like this to get all tests to run for some reason…
pub fn main(init: std.process.Init) !void {
_ = init;
_ = get;
}
$ zig build --watch
Build Summary: 3/3 steps succeeded
install success
watching 79 directories, 0 processes
Sweet! Still good. Awesome, let’s keep going!
1 hour later
Okokokok. I’m ready to commit to this design and connect it to the rest of my program. Let me actually call get from main now.
pub fn main(init: std.process.Init) !void {
_ = init;
get(.one);
}
Zig compiler: April fools!
└─ compile exe tmp Debug native 2 errors
src/main.zig:12:10: error: enum 'main.Number' has no member named 'three'
.three => "3",
~^~~~~
src/main.zig:3:16: note: enum declared here
const Number = enum {
^~~~
So. I guess Zig wasn’t really checking all of my code… it’s fast because it didn’t do anything?.. ![]()
Is there a way to tell Zig to slow down and actually check all of my code? I would be 100% OK with trading speed for actually knowing that my code builds. It kinda feels like a step back from C++ or Go, where compile == code builds/types are correct.
I’m currently in the middle of a medium-sized refactor. I’m slinging code around, writing new code on top, and I’m terrified none of it actually builds…