December 28, 2013 importing modules from a function | ||||
---|---|---|---|---|
| ||||
Hi,
A moment ago I was reading a reply to one of my posts, and noticed Artur Skawina did this:
> void main(string argv[]) {
> import std.string;
> ...
I didn't know D allowed importing modules from a function.
Now, my question is: what is the advantage of this over importing the regular way?
Regards, Hugo
|
December 28, 2013 Re: importing modules from a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hugo Florentino | On Saturday, 28 December 2013 at 14:23:17 UTC, Hugo Florentino wrote:
> I didn't know D allowed importing modules from a function.
> Now, my question is: what is the advantage of this over importing the regular way?
>
> Regards, Hugo
Several advantages:
1) It is easier to maintain the code as origins of imported symbols become obvious immediately - less chance to get unused imports hanging around after some refactoring.
2) Considerably smaller chance of name clash between symbols from different modules (can only clash if used within same function / scope)
3) If it is templated function / scope, it won't be imported unless template is instantiated. "lazy" import effect. That may improve compilation times quite a lot in certain style of code.
|
Copyright © 1999-2021 by the D Language Foundation