[Issue 10378] Local imports hide local symbols

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jan 17 12:02:55 PST 2016


https://issues.dlang.org/show_bug.cgi?id=10378

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #16 from Steven Schveighoffer <schveiguy at yahoo.com> ---
Why can't locally imported symbols work exactly as globally imported symbols,
but only accessible in the given scope? That is:

import std.stdio;
// import std.conv; // line 2
void func(string text) {
        import std.conv; // after this, it's like line 2 was included
        writeln(text); // because std.conv is imported "globally" it doesn't
                       // mask local var.
}
void main() {
        // line 2 isn't included inside here, because it wasn't imported.
        func("Hello world");
}

To me, the only benefit of using locally imported symbols is to make the
imports implementation details for that function. Having the module just
"import globally" at the right time follows the principle of least surprise
(they work like all other imports).

There's another travesty this would fix. If you import another module that
overloads a function in your current module, you have to *reimport the current
module*. i.e.:

a.d:
module a;

void foo(int a) {}

b.d:
module b;

void foo(string s) {}

void main()
{
   import a;
   import b; // MUST include this
   foo("hi"); // for this to compile
   foo(0);
}

I admit not to knowing how easy/possible this is to do with the current front
end.

--


More information about the Digitalmars-d-bugs mailing list