Scoped Imports for Structs/Classes/Template Constraints

Enamex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 1 14:30:18 PDT 2015


On Tuesday, 1 September 2015 at 21:17:10 UTC, jmh530 wrote:
> 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?

Oh no. Importing in D is a symbol table import. If you're using a 
module and it's only available in source-format to the compiler, 
then it has to compile the whole module even if you're using just 
one function (because internally the whole module is likely to be 
very tightly coupled, and usually should be, if it's under one 
name).

    import std.stdio; // makes available for calling all symbols 
in std.stdio; compiles module if not pre-compiled and if at least 
one symbol is used (not sure about the last point)
    import std.stdio : writeln; // makes available for calling 
only 'writeln()' from std.stdio; compiles module if not 
pre-compiled
    static import std.stdio; // makes available for calling all 
symbols in std.stdio but only when fully qualified; compiles 
module if not pre-compiled

As I understand it, the only difference between cases with 
different numbers of called symbols from a module is apparent 
during linking, not compilation.

Anyone can shed light on this?


More information about the Digitalmars-d-learn mailing list