Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Learning D - D File Structure


Summary


D's file structure resembles a mix of Java and C++ files. It follows this pattern:

  1. Script Line
  2. Imports
  3. Code

Script Line


For all those Posix users include this line for easy running of your files.

#!/usr/bin/dmd -run

Imports


Imports a little different in D than Java and C++ so look out. One important thing is that D uses modules of code, not packages. Phobos, the standard library, has kept the std naming convention though, which is quite nice.
To import a module simply write a comma separated list following the import statement:

import std.stdio, std.thread;

To import a specific part of a module use a colon after the module name and use commas to separate the parts you want:

import std.stdio : writef, readln;

Since these two formats of importing both use comma separated list in different ways it is bad form to combine them.

Code


After the 2 steps above put all your functions, variables, and extern calls here. Like many other programming languages D uses a "main" function to tell where to start executing a program.


Next: Your First Program