News for hackers. Stuff that matters.
Re:Zig; what I think after months of using it by mk12 on Wednesday February 05, 2025 @01:21AM (#42944605)
It’s a trade-off.

If you allow shadowing, then you rule out the possibility of the value being used later. This prevents accidental use (later on, in a location you didn't intend to use it) and helps readability by reducing the number of variables you must keep track of at once.

If you ban shadowing, then you rule out the possibility of the same name referring to different things in the same scope. This prevents accidental use (of the wrong value, because you were confused about which one the name referred to) and helps readability by making it easier to immediately tell what names refer to.

Re:Zig; what I think after months of using it by drougge on Wednesday February 05, 2025 @02:17AM (#42945017)
I think it's worth pointing out that the example in the article contains a bug caused by not having shadowing: "const foo3 = try foo.addFeatureB();" should not be using the original foo, but foo2.
Re:Zig; what I think after months of using it by pkulak on Wednesday February 05, 2025 @01:34AM (#42944703)
And on the whole, I prefer shadowing. I’ve never had a bug in either direction, but keeping everything immutable without shadowing means you spend all your brain power Naming Things.
Re:Zig; what I think after months of using it by lnenad on Wednesday February 05, 2025 @01:57AM (#42944876)
I mean that's a really fake problem. How many times per line of code do you actually need to name variables and how many of those times you're shadowing a previously defined var. I'm guessing a very small amount.
Re:Zig; what I think after months of using it by polanas on Wednesday February 05, 2025 @08:31AM (#42948205)
In case of rust, it actually happens quite often. I find myself rarely needing to use mut, instead using functional approaches such as iterators and expressions. So a high percentage of the code is let statements
Re:Zig; what I think after months of using it by lolinder on Wednesday February 05, 2025 @09:15AM (#42948738)
Exactly. Shadowing is a super important complement to Rust's immutability. Without it immutability would be less useful and therefore less used.
Re:Zig; what I think after months of using it by lolinder on Wednesday February 05, 2025 @09:14AM (#42948722)
It's not just the naming things, it's also what you do after you've named them—if you can't shadow a name then you are stuck both coming up with new names and sifting through all the existing names in your autocomplete to try to remember which one is the real one at this point in the code. Get it wrong? There's a bug.

That's not a fake problem, it's a problem I've actually run into on a regular basis on languages that don't have shadowing.

Re:Zig; what I think after months of using it by pkulak on Wednesday February 05, 2025 @12:17PM (#42951812)
It absolutely depends on the language and how heavily it encourages immutability. For example, Rust and Elixor allow shadowing.

An awkward middle ground for me is Kotlin. It allows shadowing, but warns, so it might as well not be allowed. So you end up using lots of scoping tricks to avoid either making everything mutable, or having dozens of nearly-identical variables.