importing modules from a function

Dicebot public at dicebot.lv
Sat Dec 28 07:31:58 PST 2013


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.


More information about the Digitalmars-d-learn mailing list