New guideline to place import statements as locally as possible

monarch_dodra monarchdodra at gmail.com
Fri Mar 21 03:41:36 PDT 2014


On Friday, 21 March 2014 at 10:03:20 UTC, Steve Teale wrote:
> OK, so what happens that's different when I pull in 
> std.typetuple at the point where I need it? Does that avoid 
> pulling in all the other stuff?

The difference is whether or not the import gets executed at all. 
For *you*, it doesn't make much difference where you do your 
import, it only impacts the people that are *importing you*.

A trivial example is this:

//----
module someModule;

void foo(T)(T t)
{
     import someOtherModule1;
}

void bar(T)(T t)
{
     import someOtherModule2;
}
//----

Now imagine you need to use someModule.foo. You don't need 
"someOtherModule2", since that's only needed by baz. So this 
approach means you don't import it. someOtherModule1.

//----

If the functions are not template, or in end user code, it 
doesn't change much where you do the imports, since they'll 
happen anyways. The only difference is which symbols are visible 
where.

> How does this problem manifest itself - code bloat, or slower 
> compilations, or what?

I can't answer that personally. I just know there was a pretty 
big push from the dmd guys to cleanup the phobos code. I don't 
know what the actual impacts are.


More information about the Digitalmars-d mailing list