DMD 2.086.0 introduced import std
. This is very convenient for scripts and small apps. The downsides are compile time, the resulting file size, and pollution of global namespace.
When writing scripts, the file size is no concern, you just want to get things done as fast as possible. Globally import symbols can give you some fun debugging session (Oh, you forgot to define name
? It’s time for you to learn about std.compiler : name
, baby!). But the real dealbreaker is compile time. These extra hundreds of milliseconds starts to matter when you’re prototyping your script and recompile it every few seconds. With import.std
you lose more time than you save by not importing things manually.
But if you think about it, there’s only a small portion of Phobos that you want to have in the most cases. So I have an idea of some std.essentials
module that would publicly import the most commonly used Phobos modules like std.traits
, std.range
, std.algorithm
, std.conv
etc.
To try it out, I created a file that looks like this:
module essentials;
public {
import std.traits;
import std.range;
import std.algorithm;
import std.array;
import std.string;
import std.typecons;
import std.stdio;
import std.conv;
import std.format;
import std.exception;
}
I’ve tried it with a hello world (only uses writeln
) and a more complex app that makes use of std.algorithm
and such both at runtime and at compile time. Results:
Compilation time, ms (measured with Windows PowerShell Measure-Command
):
manual imports | import essentials; |
import std; |
|
---|---|---|---|
hello world | 176 | 233 | 610 |
test app | 411 | 410 | 738 |
File size, KB:
manual imports | import essentials; |
import std; |
|
---|---|---|---|
hello world | 262 | 262 | 637 |
test app | 327 | 327 | 663 |
I didn’t do more rigorous tests but this already looks very promising.
This also deals with namespace pollution, we are only importing the most common and well-known stuff from Phobos.