A Beginner's Guide to D/The Basics/Introduction to Modules

From Wikibooks, open books for an open world
Jump to: navigation, search


(Java programmers: Please read the last subsection of this chapter)


What are modules?[edit]

Technically, modules group things which belong together logically. A module can contain any number of definitions for functions, variables, and other D constructs which will be introduced later. These parts can then be easily accessed by other modules in D.

Each D program consists of at least one module which contains the main() function (see previous section).

Practically, in D a module is just a file with D source code (which has normally the file suffix ".d").

What are they good for?[edit]

Reusable Code[edit]

In programming, making code reusable is very important. Without reusable code, the process of writing even a fairly trivial program would be overwhelmed by the tasks of rewriting all of the basic code needed, such as communication with the user or network and mathematical functions.

Overview[edit]

Similar to functions, modules are another way to cut down a complex program into smaller parts which can be looked at separately.

Importing[edit]

In D, to access a module X from module Y, module X must be imported in module Y.

Importing a module is achieved through an import statement, consisting of the keyword import and the name of the module. For example, to import the module 'std.cstream' (which will be discussed later), the following statement is used:

import std.cstream;

Of course, this can be generalized to import any module:

import module name;

Everything defined in the imported module (e.g. functions, variables, etc.) become known to the importing module and can be used there.

Packages[edit]

A package is a group of modules and possibly (sub)packages. Practically it is just a directory containing module source files and other directories. In the example above, "std" is a package containing the module "cstream" (among others). When importing a module, the package(s) containing it must always be mentioned in the import statement, separated by dots from each other and from the actual module name (very similar to the path to a file).

Packages are usually used to group together modules that handle one or more commonly-performed tasks. This type of package is known as a library. Most libraries specialize in doing a single task, but some are (much) more general. The Phobos Runtime Library is a notable example of the latter category. It is the D language's standard library and comes standard with all D compilers as the std package. It handles basic features not built into the language itself, such as input and output, common math functions, and much more. Although D compilers are required to include Phobos (or a 100% compatible library), it is not a part of the language itself. For that reason this book will not cover Phobos in-depth beyond what is necessary.

Writing Modules[edit]

It is of course possible to write your own modules. In fact, any D program you write will be contained in a module. However, dividing your programs into multiple modules will not be introduced in this book until several more fundamentals are introduced.

Info for Java programmers[edit]

  • Modules can contain zero or more class definitions and can also contain other entities, like e.g. functions.
  • There is no relation between the module name and the name of contained entities.
  • Module names follow the general rules for identifiers, the filename suffix is ".d".
  • Module can be organized in packages (using directories) as in Java.