Pathological import symbol shadowing

Walter Bright newshound2 at
Wed Nov 18 01:11:35 UTC 2020

On 11/15/2020 6:24 AM, Timon Gehr wrote:
> Straw man.

Doing "import std;" is just asking for trouble. I argued against it for years, 
and yet it was slipped in a few months ago without my knowledge. Having 
everything in Phobos in scope is just causing trouble. For just one of the 
problems, it prevents adding new modules to Phobos as it can break existing 
code. For another, it turns the world's fastest compiler into a pig.

The example given was another example of why it's a bad idea, as the more names 
there are the more the likelihood of a collision.

> I think it's safe to say that the snippet was written to illustrate a point and 
> to focus on anything else to the detriment of a discussion of that point is just 
> deflection.

Teoh would have never run into this issue if he hadn't used import std;

I don't blame Teoh, a user should expect that import std; should not cause 
problems although the problems are inevitable. It should NEVER have been added 
to Phobos.

> If the language was working properly, the code would compile and run fine. There 
> is a hole in the design of the language here. There's no need to prioritize this 
> particular issue, but I don't understand why you don't just acknowledge that 
> this is not how the compiler should behave in this situation.

Frankly, I don't know how the compiler should behave. The original import scheme 
was simple, sound, and easy to explain, but everyone complained it wasn't 
"intuitive" and put in a complex scheme full of special cases. Complex special 
cases produce an endless stream of unanticipated special cases.

Altering how the compiler does lookups now, regardless of how it "should" 
behave, could break existing code in mysterious ways. Especially if one imports 
every symbol in existence into a local scope :-(

> It's not just rejects-valid either, this issue has accepts-invalid cases:
> ---
> import std.stdio;
> string readAndLog(string filename){
>      import std.file;
>      auto text=readText(filename);
>      write(filename," read successfully!\n");
>      return text;
> }
> void main(){
>      writeln(readAndLog("important_data.txt"));
> }
> ---

Please elaborate on what is invalid about it? Don't make me guess!

More information about the Digitalmars-d mailing list