Hacker News new | ask | show | jobs
by saithound 499 days ago
Shadowing always has been a feature, doubly so in languages which lack linear types.

It is a promise to the reader (and compiler) that I will have no need of the old value again.

Notice that applying the naming convention you suggest does nothing to prevent the bug in the code you quoted. It might be just as easy to write

const initial_foo = Foo.init(); > const foo_feature_A = try initial_foo.addFeatureA(); > const foo_feature_B = try initial_foo.addFeatureB();

but it's also just as wrong. And even if you get it right, when the code changes later, somebody may add const foo_feature_Z = try foo_feature_V.addFeatureX();. Shadowing prevents this.

1 comments

Said promise should also be checked for sanity. E.g.

  for i in range(N) {
    for i in range(M) {
      # Typo; wanted j.
      # The compiler should complain.
    }
  }
The Rust compiler would complain in this case that the initial i variable is unused. Unused variables should be named with an underscore, _.