Hacker News new | ask | show | jobs
by suby 552 days ago
Zig's compiler error on unused variables is a deal breaker for me unfortunately. The language author also has an insulting attitude towards anyone who wants otherwise, categorizing them as bad programmers he doesn't want to work with and doesn't want using Zig.

The compiler error on variables which are mutable when could be const is almost as annoying. Zig does not acknowledge that not all code is production code, that sometimes you want to prototype without having to backtrack and fix compiler errors to irrelevant things when the goal is prototyping and figuring out what you are building.

It also adds friction to learning the language because statements you write will immediately get flagged as wrong -- is it actually wrong due to your unfamiliarity with the language, or is it just the lsp immediately flagging and underlining all unused as red. Better take a moment to check.

Super annoying to me. I can't get past it, nor do I trust the author of Zig to not go even further in this direction. He has made it clear he will not compromise on this issue.

There are more planned similar errors on the way here. At least it looks like they are no loner planning to do compiler errors on unused pub functions if they are not accessible from outside the package.

Zig: Trust the programmer to manually manage memory, but not to clean up unused variables.

The language is weirdly pedantic, in ways orthogonal to Rust.

6 comments

> Zig: Trust the programmer to manually manage memory, but not to clean up unused variables.

That's such a good tagline, actually one of the things that annoyed me with zig as well. The other one is the lack of attention to syntax aesthetics and ergonomics, it's a bit all over the place and I am also not fond of semicolons.

The worst part is if you have an unused variable the compiler won't even show you the real errors until you fix all the warnings first.

> Compile error: This variable of type `Foo` is unused

Ok then, I'll just remove that, and...

> Compile error: This function you're calling expects an argument of type `Foo`

Just kill me.

Odin allows you to opt into the vetting tools through build flags (`-vet` and `-vet-*`) or even per-file build tags `#+vet unused`.

This is one thing that really annoys me about Go, I am a competent programmer and I don't want be treated like an incompetent one. If I want vetting tools, I'll enable then when it suits me.

Some people love this, some think it’s a necessary sacrifice - a rite of passage to endure in order to produce ”optimal” software.

Others try Odin or C3 instead.

Go refuses variables that are declared and not used as well.

And honestly, I like the fact that it forces the source code to not include bloat. Lines of code that are there, but don't do anything, and can be misleading.

And in Zig, the language server can be configured to automatically add and remove `_ = variable;` statements, so this is frictionless.

Meanwhile in Haskell they introduced typed holes in order to allow you to compile more types of programs that aren't yet fully finished.

I'm all for preventing this kind of thing from making it into the main branch, but a compiler error by its nature blocks compilation, which forces weird shenanigans like commenting out lines of code or adding arbitrary meaningless uses. Why is that better than a compiler warning (made mandatory in CI)?

Yeah, in C, you at least add the unused attribute and can grep for it. In some languages that do not support "_" prefix or the attribute meaning unused, you have to comment out parts. It is really annoying and I think it is worse.

In some C code where I have to suppress an unused variable warning (for example to a pthread callback), I just do (void)arg at the top of the function.

The solution is easy, have separate debug and release modes and only mandate variables to be used in the latter (though still provide a flag to disable the check, for the occasional debugging an issue that only happens with the release config).

Alsl, automatic `_ = var` is the worst of all, now instead of the compiler showing me one-by-one the few variables I'm not using temporarily so I can fix those, I instead have made n modifications all across the codebase (remember, this is all recursive) that are syntactically correct and won't be shown by the compiler.

Zig already has a seperate debug mode, if you feel the issue is important, file an issue on the zig GitHub page
Given that this is quite a controversial topic, I don't think they would allow such issues, besides the currently existing ones.
I prefer adding "_" in front of unused variables, like I do in OCaml.
It's stupid and annoying in Go too!
"Let me comment this line out"

"Oh and I have to do this one too"

"Oh and this five as well"

"Almost there"

"Nope"