5 ms·
I am not a fan of zig, but I am a fan of discipline so I like this particular design decision.
by norir 2y ago
I am not a fan of zig, but I am a fan of discipline so I like this particular design decision.
- alpaca128 2y agoI would be fine with it if it only threw an error about that when building in release mode or if there was a flag to silence it temporarily. But while trying out some things and learning the language I find it annoying. And I don't know how it makes me more disciplined when I can just write `_ = unused;` to suppress the error. Even worse, if I forget that assignment in the code the compiler will never warn me about it again even when I want it to. So far I haven't seen any upside to this.
- dgfitz 2y agoI feel like this is like saying “python shouldn’t care so much about indentation, I’m just trying to learn!”
- Phil_Latio 2y agoWrong syntax is (and must be) an error. Totally different. The Problem in Go and Zig is that they put theory over practice: No compiler warnings is a good idea in theory, but fails in practice for things like unused variables or unused imports. Defending that makes it even worse and begs the question what other treasures they have burried in their language design. This thread is a testament to that.
- riwsky 2y agoForbidding unused imports was a direct response to the practical difficulty of compiling google-scale C++ binaries: https://go.dev/talks/2012/splash.article#TOC_5 https://go.dev/talks/2012/splash.article#TOC_5. In theory, programmers can just be disciplined enough or set up CI lints for unused imports. In practice…
- swiftcoder 2y agoI'm sure the rest of us all benefit from arcane doctrine required to scale up a 25,000 engineer, 40,000 commit/day monorepo.
- throwawaymaths 2y agoYou do. The zig compiler and stdlib can iterate faster across a team of mostly by numbers volunteer developers with varying degrees of skill and across global timezones because of the discipline that the compiler imposes.
- em-bee 2y agoi prefer a language that doesn't even need to declare imports. why can't the compiler figure them out on its own?
- 8n4vidtmkvmk 2y agoI think you're being sarcastic, but ambiguity is the obvious answer. Your IDE can help you resolve these though.
- em-bee 2y agoi am absolutely serious. pike for example does not need imports. if a reference is not found in the local namespace the compiler will find it in the module path and resolve it by itself. there is no ambiguity because each module has a unique name. we accept type inference but don't do the same for module references? why? pike does have an import statement, but its effect is to include all members of a module into the namespace instead of just resolving the ones that are really used. and instead of speeding things up, using import on modules with lots of members may actually slow things down because the namespace is loaded up with lots of unused references. sometimes import is used to help readability, but that's rarely needed because you can solve the same with a simple assignment to a variable. if you can show me an example where import resolves an ambiguity, i'll try to show how pike solves the problem without import.
- 082349872349872 2y agoI'm not sure I see the total difference between matching parens and matching defs and refs. Sure, saying "an open paren must have a matching close" is quantitatively different from "a def must have at least one matching ref", but is it really qualitatively different?
- xigoi 2y agoA language that allows you to arbitrarily omit parentheses would be impossible to parse. That’s not the case for unused variables.
- 082349872349872 2y agoNot impossible: some lisps allowed arbitrary omission of close parens (upon finding a closing square bracket). https://news.ycombinator.com/item?id=29093339 https://news.ycombinator.com/item?id=29093339 http://arclanguage.org/item?id=20979 http://arclanguage.org/item?id=20979
- em-bee 2y agoHTML allows to omit closing tags for <p>, <li> and others
- 8n4vidtmkvmk 2y agoThat's a very different thing and you know it. Indentation is syntax. You can't just omit braces and expect it to parse.
- dgfitz 2y agoZig treats unused variables as a syntax error.
- smallstepforman 2y agoLets say I gather Diag data, which I conditionally print during testing. Are you saying that I cannot leave the diag code in place after I comment out the print function? Thats unproductive and a major obstacle to using Zig. I’m still pissed at Andrews stance of preventing Tabs, operator overloading, polymorphism, and this just seals my “stay away” stance. I really do want to like Zig, but cannot.
- kcbanner 2y agoYou don't need to comment out the print function - it could gate its behavior on a comptime-known configuration variable. This would allow you to keep your debug variables in place.
- 8n4vidtmkvmk 2y agoIt doesn't need to though. It goes out of its way to determine that the variable is unused.
- alpaca128 2y agoYou are comparing invalid syntax to a purely cosmetic temporary non-issue.
- norir 2y agoThe way I deal with this in my language, which also bans unused variables, is simple: I delete the unused variable or I use it. My workflow is probably very different from yours I'm guessing. I have my editor configured to save on every keystroke and I have a watch process that then recompiles my code. I pretty much never manually compile. My compiler is sufficiently fast that I almost never wait more than 1 second to build and run my code. I notice every error immediately and fix them as they arise. This is what I am talking about with discipline. I never allow my code to get into an unexpectedly broken state and don't need a linter since I just make the compiler as strict as I would make the linter. This ultimately simplifies both the compiler and the build pipeline. These are all huge upsides for me. The cost of occasionally deleting a definition and then restoring it are for me minor compared to the cost of, say, writing a separate linter or adding feature flags to the compiler (the latter of which doesn't fit into my workflow anyway since I auto compile).
- xigoi 2y agoThe problem is that in order to delete an unused variable, you may need to delete a huge chunk of code which would be useful later when you want to actually use the variable.
- em-bee 2y agocan you give an example please? i can't imagine how any section of code would be affected by removing an unused variable. if the code reference the variable, it would be used. if it doesn't, then why would you have to delete it?
- xigoi 2y agoIn pseudocode: a = input() b = f(a) c = g(b) d = h(c) If you delete the unused variable d, then c will be unused, so you’ll have to delete it too. Iterating this, you will end up deleting the entire code.
- throwawaymaths 2y agoYou could: - comment it out - use git - use ctrl-z
- flohofwoe 2y agoJust use an editor with language server support and you don't need to worry about adding or removing the `_ = unused; // autofix`. I wrote a 16kloc Zig project a couple of months ago, and not once the 'unused variables are errors' thing was annoying (https://floooh.github.io/2024/08/24/zig-and-emulators.html https://floooh.github.io/2024/08/24/zig-and-emulators.html) IME unused variables being (linter) errors also isn't all that unusual in the Javascript/Typescript world. It feels 'normal' fairly quickly.
- alpaca128 2y agoOr the compiler could just add a flag and not make assumptions about my setup or workflow. Also linters are optional and under my control. Meanwhile the Zig compiler is forcing that onto me, and for what benefit exactly? > I wrote a 16kloc Zig project a couple of months ago, and not once the 'unused variables are errors' thing was annoying That's great, but different people are different. I've tried learning Zig twice by now but this is just a dealbreaker, simple as that.
- kaimac 2y ago"just"
- amelius 2y agoIf you're a fan of discipline then you could also just call lint (or equivalent) before compiling.
- em-bee 2y agodoesn't help me when i have to deal with other peoples code. a language that enforces discipline by itself tends to be easier to read.
- amelius 2y agoBut what is the next step? A compiler that complains when you multiply by constant one?
- em-bee 2y agoanything a linter can do, can be included in a compiler. or the linter can be part of the compilation process by default. iaw instead of being optional it should be required maybe with a special opt-out, but opt-out should be frowned upon.
- fuzztester 2y agoif you are a fan, you cannot be disciplined, because that is a contradiction in terms. fans are indisciplined. ;)