Scoped Imports for Structs/Classes/Template Constraints

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 1 14:17:07 PDT 2015


On Tuesday, 1 September 2015 at 19:48:02 UTC, Enamex wrote:
>
> They aren't selective, yeah. But the rationale is good: There's 
> not supposed to be any way to import modules with the same path 
> so static importing means it's entirely and always unambiguous.

I understand that a static import is always unambiguous. That's 
really the only thing the documentation says about static 
imports. I wonder if there are any other difference.

Consider these three different ways to import std.stdio
import std.stdio;
import std.stdio : writeln;
static import std.stdio;
and suppose writeln is the only function in std.stdio the program 
is using.

In each case, the size of the exe is exactly the same. So 
presumably, the compiler is smart enough to see that 
std.stdio.chunks, for instance, isn't used and not to include in 
the final program. However, there might be a difference in 
compile times (though I don't exactly know how to measure that).

My understanding is that import std.stdio; leads to all of 
std.stdio being compiled, but import std.stdio : writeln leads to 
only writeln being compiled (and anything it depends on). There 
are also faster lookups during compilation, or something, I 
think. Given that the documentation only indicates the difference 
between import std.stdio and static import std.stdio is the 
ambiguous-ness that you point out, I would expect that the time 
to compile would be the same as import std.stdio. Is this correct?


More information about the Digitalmars-d-learn mailing list