Local imports hide local symbols

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 23 12:16:16 PDT 2014


On Tue, Sep 23, 2014 at 12:06:26PM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
> On 9/23/14, 12:01 PM, H. S. Teoh via Digitalmars-d wrote:
> >On Tue, Sep 23, 2014 at 06:56:56PM +0000, Meta via Digitalmars-d wrote:
> >>On Tuesday, 23 September 2014 at 18:52:13 UTC, H. S. Teoh via Digitalmars-d
> >>wrote:
> >>>I can think of a few:
> >>>
> >>>1) Change lookup rules so that symbols pulled in by local import
> >>>are found last. Walter has stated that he disagrees with this
> >>>approach because it complicates symbol lookup rules.
> >>>
> >>>2) Emit a compile error if any symbol pulled in by the local import
> >>>shadows a symbol currently in scope, according to the same rules as
> >>>declaring local variables that shadow identically-named variables
> >>>in an outer scope within the current function body.
> >>>
> >>>3) What you proposed in the bugnotes: only allow `static import
> >>>xyz;` and `import xyz : a, b, c;` at local scope.
[...]
> >>>2b) Allow unqualified `import xyz;` in local scope, but only if
> >>>NONE of the imported symbols shadows ANY symbol visible from that
> >>>scope.
> >>>
> >>>
> >>>T
> >>
> >>The only tenable option from that list seems to be 1. 2 and 2b
> >>either compile or not depending on the implementation details of the
> >>module (i.e., add a symbol i to a module and it may break code in an
> >>entirely different module that imports your module), and 3 will
> >>break a lot of valid code.
> >
> >Good luck convincing Walter, then. :-(  Or maybe if we can convince
> >Andrei to twist his arm hard enough... :-P
> 
> This is a gaping hole that gets worse by the minute. We must fix it
> with the next release. -- Andrei

Here's another idea:

4) Allow unqualified imports at local scope (even if imported symbols
will shadow currently symbols in scope), but emit a compile error if
such ambiguous symbols are referenced. So, this would be allowed:

	----mod.d----
	module mod;
	string w, x, y;

	----main.d----
	void main() {
		int x, y, z;
		import mod;

		w ~= "a";
		z++;
	}

But this would cause a compile error:

	----mod.d----
	module mod;
	string x, y;

	----main.d----
	void main() {
		int x, y, z;
		import mod;

		x++;	// Error: ambiguous symbol 'x', could be local
			// variable 'x' or mod.x
	}


T

-- 
He who laughs last thinks slowest.


More information about the Digitalmars-d mailing list