Andrei Alexandrescu:

I'm not sure whether shadowing globals or members would be a good idea.

I understand. It's not a clear cut topic. There are real reasons both for and against this idea. But surely it should be considered. I have not yet (re)opened an enhancement request on this.


gcc has a -W flag for that, and someone tried to turn it on at Facebook but with debatable results. We ended up not using that warning.

In the example I have shown it's with() that is uncovering a struct field with the same name of a module-level name. So it's not exactly the same as a local variable shadowing a module-level name (and currently with has anti-hijacking for local variables). I think that shadowing module-level names with with() is not good. Also because of this idea:
https://d.puremagic.com/issues/show_bug.cgi?id=6917

Regarding Facebook, I presume that Gcc flag was applied to plenty of already written code. If you apply it since the start of a new project perhaps (probably) its effects are different. This detail is important, because lot of D code is yet to be written.

Regarding more generally the topic of shadowing module-level names with local names, the attribute "pure" helps avoid some cases, because you can only shadow immutable global names, and this is a little less dangerous/troublesome.

Time ago I suggested an optional @outer() attribute, that's useful to specify what names from outer scopes a function/method is allowed to see and use. It looks a bit excessive, but it shows that I have hated bugs caused by shadowing globals silently.

An IDE could underline the shadowing variables, turning the silent shadowing into a visible one, without giving warnings (it's a kind of much noisy warning), but unfortunately a compiler like dmd can't do that.

Another problem is that a command like "import std.algorithm" imports lot of names in the current module, and this could cause many false alarms. But this could be a good thing, and it can push Phobos devs to add more "private" tags to Phobos names, and D developers to add more qualified imports in their programs.

Bye,
bearophile

Reply via email to