Symbol lookup rules and imports

via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 3 04:07:38 PST 2014


On Tuesday, 2 December 2014 at 22:02:23 UTC, H. S. Teoh via 
Digitalmars-d wrote:
> 2) OK, so the conclusion is that unqualified scoped imports are
> dangerous, right? Alright, let's see what happens when we use 
> qualified
> imports:
>
> 	// mod.d
> 	module mod;
> 	struct S {
> 		// Use a fully-qualified import.
> 		// We place it in the body of S because S's methods
> 		// repeatedly needs it -- after all, DRY is good, right?
> 		import std.format : format;
>
> 		void method1(string fmt) {
> 			writeln(format(fmt, ... ));
> 		}
>
> 		void method2() {
> 			auto s = format("abc %s def", ...);
> 			...
> 		}
> 	}
>
> 	// main.d
> 	module main;
> 	import mod; // we need the definition of S
>
> 	void format(S s) {
> 		... /* do something with s */
> 	}
>
> 	void main() {
> 		S s;
> 		s.format(); // UFCS -- should call main.format(s), right?
> 	}
>
> Wrong. That last line in main() actually calls 
> std.format.format. Why?
> Because in mod.d, the `import std.format : format` declaration 
> actually
> pulls in std.format.format into the symbol table of S, 
> therefore,
> S.format now refers to std.format.format. This, therefore, 
> hijacks the
> intended call to main.format.
>
> This is: https://issues.dlang.org/show_bug.cgi?id=13808

Surely this can't be intended behaviour? It seems its imported 
publically instead of privately, so that it gets "reexported" to 
outside users of the struct. If so, I don't think we should add 
workarounds to std.datetime or any other places where this could 
occur.


More information about the Digitalmars-d mailing list