News for hackers. Stuff that matters.
Re:Zig: The Modern Alternative to C by kaba0 on Friday March 10, 2023 @05:14AM (#35092618)
Does Zig still have that insane compiler enforced no unused variables policy? Honestly, that is such a stupid thing that it literally kept me away from the language, which I otherwise find interesting for its niche.
Re:Zig: The Modern Alternative to C by flohofwoe on Friday March 10, 2023 @05:27AM (#35092692)
The feature is really quite ok when working in an IDE where those errors are displayed immediately as error squiggles, and there's now also an error for any 'left-over'

    _ = unused_variable;
...statements (although I'm starting to prefer Typescript's convention to mark unused variables and function parameters with a leading underscore to suppress unused errors).

In any case, once I started to write more TS code where the TS compiler and linters are usually configured to also catch any unused variables as "errors" I no longer see Zig's stance on the same thing as critical as before, it's more or less "just" a tooling issue.

Re:Zig: The Modern Alternative to C by kaba0 on Friday March 10, 2023 @05:32AM (#35092730)
But why should I have to recursively modify my code when I am experimenting with shit? Like, am I the one that does something wrong, because commenting/uncommenting a single line to check the outcome of a program is extremely common in my workflow, and that fkin compile error can absolutely throw me out of the loop of what I was trying to do.

And it is not even hard to solve, just add a “production” profile where it is an error for all I care, and a “debug” one where it really shouldn’t be linting my code unnecessarily.

Re:Zig: The Modern Alternative to C by flohofwoe on Friday March 10, 2023 @05:53AM (#35092875)
To be clear, I would also actually prefer if those were warnings in development mode, but so far at least Zig has no warnings, only errors. But I'm open to the idea that the Zig team knows what they are doing ;)
Re:Zig: The Modern Alternative to C by tialaramex on Friday March 10, 2023 @09:25AM (#35094615)
AIUI Zig has embraced the view that either something is bad, and so it's an error and we shouldn't compile, or it's fine so no need for a warning. I don't think this actually is a reasonable stance although I can see why C might make you think so.

Rust's linter, clippy, provides a rich seam of stuff that definitely shouldn't be fatal but some / many people would value knowing about. For example yesterday I built a Range which intentionally is the wrong way up, isize::MAX..isize::MIN - and so clippy says well, that's probably not what you wanted. This is a good lint. If I ever do the same thing by mistake I definitely want to know about it, but this time was not a mistake, so I wrote an allow attribute on that Range to silence the linter and also flag to readers, "No, this is on purpose".

Re:Zig: The Modern Alternative to C by Gigachad on Friday March 10, 2023 @05:30AM (#35092713)
Really seems like something that should be a lint rule..
Re:Zig: The Modern Alternative to C by mekoka on Friday March 10, 2023 @05:17AM (#35092633)
I'm waiting for the first fork that will do away with that "feature".
Re:Zig: The Modern Alternative to C by markisus on Friday March 10, 2023 @10:30AM (#35095585)
It already exists. You’ll just have to compile it yourself which is relatively easy.

https://github.com/markisus/unzig

Re:Zig: The Modern Alternative to C by tialaramex on Friday March 10, 2023 @05:49AM (#35092848)
Product of the creator's will. Andrew is confident that always rejecting programs with unused variables catches bugs, and he believes that if you don't like this you should have software to adapt the Zig you write so that it compiles, by adding or removing _ = unused_variable; style statements to consume the otherwise unused variables.

There can be a benefit to a singular coherent vision behind something - committees can produce languages which don't do anything well by compromising everywhere - but one of the negatives of visionaries is that they can stubbornly stick to their preferences overriding any other consideration. Andrew is quite sure he's right, nothing will overcome that.

It's OK, C++ manages to suffer from afflictions of a single coherent vision (Bjarne Stroustrup's) and having an unwieldy committee structure (WG21 the "C++ Standards Committee") and despite the resulting flaws it was enormously successful.

Re:Zig: The Modern Alternative to C by banyaaa on Friday March 10, 2023 @08:15AM (#35093949)
What's an example of a bug that would be caused by having an unused variable?

I honestly can't think of one.

Re:Zig: The Modern Alternative to C by eska on Friday March 10, 2023 @08:42AM (#35094209)
Store an error code forget to check it, store a future but fail to poll it, etc.
Re:Zig: The Modern Alternative to C by kaba0 on Friday March 10, 2023 @08:53AM (#35094305)
Which is absolutely not solved by assigning it to _ neither, if anything it will just make the variable appear used in syntax highlighting and that will make me forget to properly use it!

IDEs just gray out the unused variables and that is an 1000x better way of handling this issue.

Re:Zig: The Modern Alternative to C by eska on Friday March 10, 2023 @09:12AM (#35094472)
I think you misunderstand the purpose of assigning to _.

Extremely common bug 1: a function returns an error/future, but you don’t check/poll it “foo();”. May happen because you copy some tutorial code that’s not rigid about error checking, or you don’t realize that this language doesn’t have futures that run without being polled.

Quite common bug 2: you store the error/future, but don’t check/poll it “let a = foo();”. May happen when moving code, certain branches, copy paste error, not enough coffee.

Opt-in: it doesn’t matter if the call succeeded or the future is polled, leave me alone “let _ = foo()”. May happen in test code for example. This is not the common case, so the opt-in annoyance is justified to improve the common case.

Re:Zig: The Modern Alternative to C by kaba0 on Friday March 10, 2023 @10:28AM (#35095542)
You misunderstand me - I’m talking about my original problem of an unused variable that happened due to me commenting out something for example. Temporarily unused variable if you will.

Without recursively commenting out any further variable that have also become unused by my action (which I hope we can all agree is extreme tedious and error prone), I am left with no choice but assigning it to _, which as you mentioned already have a normal usage, making it later very hard to discern which is just a temporarily unused variable, or a deliberately ignored one. The very “feature” can cause bugs, besides being annoying as hell.

Re:Zig: The Modern Alternative to C by rom-antics on Friday March 10, 2023 @09:18AM (#35094538)
I agree. I gave Zig a fair shake, even went so far as to find the compiler PR that automatically adds `_ = foo` to your source code while compiling[1] and set up the VSCode extension with autofix. I couldn't get used to seeing the lines with `_ =` appear and disappear all over my code while I was typing.

With the usual warnings approach, you can rely on compiler/IDE/pre-commit tooling to find unused variables - they'll all nag you until it's fixed. With Zig's autofix approach, those problems are immediately silenced and you don't have any help from the compiler or tooling to find unused variables. It's quite an ironic outcome if you think about it.

[1]: https://github.com/ziglang/zig/pull/12803

Re:Zig: The Modern Alternative to C by properparity on Friday March 10, 2023 @03:26PM (#35100443)
That is something a decent type system should solve - make it impossible to pass 'incomplete values' on, so any state further on which depends on the error handled/not handled will expect the appropriate type and compiler will error at that call site.

An unused variable means you weren't passing it on anywhere so there is no code which depends on its value, so how can it be a bug?

future = x.do_async(); return;

should not error out because of 'unused variable', it should give a error message concerning the lifetime of the future object.

Re:Zig: The Modern Alternative to C by afiori on Friday March 10, 2023 @09:05AM (#35094413)
using the wrong variable in repeated code:

value1 <- f()

value2 <- f()

g(value1)

g(value1)

or other similar cases

Re:Zig: The Modern Alternative to C by tcmart14 on Friday March 10, 2023 @12:05PM (#35097277)
As someone who works at a company with an old horrible code base, for me its not so much of it can cause bugs, but it presents people from doing stupid things. In parts of a codebase we have a long legacy function that contains a string that tries to show what "state" a process is in, but the string is only ever used for that. Never is it actually used by the system. So we have code that looks like this

  public void processTransaction() {
    string state = "start";
    getCardDetails();
    state = "serialize";
    serializeCardData();
    state = "send data";
    sendData();
    state = "check return code";
    bool success = checkReturnCode();
    if (!success) {
      state = "failed";
      doFailThing();
      return;
    }
    state = "store transaction record";
    storeTheThing();
    state = "complete";
  }
First, this code is a simple example, in reality, the code has bunch of branching and doesn't actually call out to functions to do things like "getCardDetails," so just replace that function in the example above with some parsing logic of a string to parse Track1 and Track2 data. The equivalent method we actually have in our code base to do that is ~1500 lines of code. But that string is doing nothing that a comment couldn't accomplish. Or more importantly, what the code could describe itself if it actually adhered to good design principles. Ideally, I would refactor this, but the owner of the company is adament on keeping this 1500 line abomination untouched.

For me, I often unused variables in production code are usually filling the void a comment or good design would have filled.

Re:Zig: The Modern Alternative to C by tialaramex on Friday March 10, 2023 @03:44PM (#35100750)
The good news is that although intuitively that feels horrible, mechanically those are string literals, so, that doesn't actually do very much, just re-assigning a pointer. Moreover, there's no way an optimising compiler can't see those assignments are futile and elide them, whereupon in release builds it might as well be a comment.

And yes, lots of people who don't like Zig's choice agree unused variables are bad and shouldn't survive into your release code. They just don't agree with Andrew that it's a fatal error and the program shouldn't build.

Re:Zig: The Modern Alternative to C by HeavyFeather on Friday March 10, 2023 @09:57AM (#35095057)
“Insane” really? How so? In what world do unused variables make sense?
Re:Zig: The Modern Alternative to C by infamouscow on Friday March 10, 2023 @10:45AM (#35095850)
I think forgetting to assign a variable to the return value and ignoring the return value are two different things.

Zig regards forgetting to assign a variable to a return value as a compile time error because there's an entire class of bugs that stem from it, e.g. resource leaks.