Imports and modules

One of D's core design decision was to be consistent and avoid corner cases in the language. This is called turtles all the way down. One good example for this consistency are imports.

Imports

For a simple hello world program in D, imports are needed. The import statement makes all public functions and types from the given module available.

The turtles start falling down

An import statement does not need to appear at the top of a source file. It can also be used locally within functions or any other scope. In the following chapters you will see that this applies to almost all concepts in D. The language doesn't expose arbitrary restrictions on you.

Selective imports

The standard library, called Phobos, is located under the package std and its modules are referenced through import std.MODULE.

The import statement can also be used to selectively import certain symbols of a module:

import std.stdio : writeln, writefln;

Selective imports can be used to improve readability by making it obvious where a symbol comes from, and also as a way to prevent clashing of symbols with the same name from different modules.

Imports match directories and files

D's module system — in contrast to other systems — is entirely based on files. For example, my.cat always refers to a file cat.d in the folder my/. The folder my needs to be in the current working directory or in one of the explicitly specified directory imports (-I). Lastly, to ease splitting big modules up into multiple smaller files, instead of cat.d, a folder cat/ could be used as well. The D compiler would then try to load my/cat/package.d instead of my/cat.d.

The convention (but not a hard rule) for package.d files is to publicly import all other modules in the same folder.

rdmd playground.d