DIP Idea: Lazy Imports
I’d like to propose a DIP for adding lazy imports to D.
Motivation
D already has the lazy
keyword for function parameters, which works wonders:
- Arguments are only evaluated when (and if) actually used.
- This avoids unnecessary work, prevents allocations, and gives a nice middle ground between eager and manual
delegate
passing.
It’s a proven feature that shows how well deferred evaluation fits into the language.
Currently, though, import
in D is eager: all imported modules are parsed and semantically analyzed during compilation, regardless of whether their symbols are actually referenced. This can cause unnecessary compile-time costs, especially in large projects with deep dependency graphs.
A lazy import would extend the same philosophy of deferred evaluation to the module system: the compiler would defer full analysis of a module until it is actually needed (when a symbol from it is referenced). This reduces compile times and makes dependency management more efficient.
Proposed Syntax
lazy import std.big;
This would mean:
The compiler does not fully load/parse std.big
at import time.
If no symbols from std.big
are used, the module never impacts compilation.
If symbols are referenced, the compiler processes the module as usual.
Alternatively, the same effect could be achieved with an attribute-style approach:
@lazy import std.big;