Monthly Archives: January 2016

Using D with Eclipse

Before continuing, make sure you have installed:

  1. A D compiler (DMD, GDC, LDC)
  2. dub (D’s package manager)
  3. Java
  4. Eclipse

1. Open Eclise

Open eclipse. You will be presented the following screen (or similar, if you ‘re using a different version).
eclipse_welcome_screen
eclipse Mars 1 welcome screen

2. Install the DDT plugin

Install the DDT plugin by following the instructions here.

3. Create a project

3.1 Create project

To create a new project, select File → New → Project...

3.2 Select project type

eclise_new_project_0
Expand the D section and select DUB project

3.3 Set project name

eclise_new_project_1
Enter a name for the project

That’s it, the project has been created!

4. Configure the plugin

By default, the editor does not show auto-completion as you type (only when you press .).
To enable it, select Window → Preferences

Expand DDT and then Editor from the menu on the left. Check Enable alphabetic characters as trigger.

eclipse_autocomplete_settings
Check Enable alphabetic characters as trigger to enable syntax auto-completion as you type.

5. Run the project

To run the project, select Run → Run.

However, the first time you try to run, an error will occur “The selection cannot be launched, and there are no recent launches.”. To fix this, select Run → Run As → D Application.

6. Debug the project

Eclipse can debug D applications and provides a nice GUI for viewing the call stack, values of variables etc.

6.1 Setting a break point

To set a break point, double click on the desired line number in the editor.

6.2 Debugging

To debug, select Run → Debug. Eclipse will prompt you to switch to debugging mode – select yes.

eclipse_debuggin
Eclipse’s debug perspective. Call stack is located at the top left, values of variables at the top right.

Using D with CodeBlocks

Before continuing, make sure you have installed:

  1. A D compiler (DMD, GDC, LDC)
  2. Code::Blocks IDE. If you are on a Linux system, Code::Blocks should be in your distribution’s default packages. E.g. on Ubuntu, you can install it by executing sudo apt-get install codeblocks. Otherwise, download and install it from the Code::Blocks website.

1. Create a Code::Blocks

If Code::Blocks is installed on your machine, you will see the following screen:
codeblocks_welcome_screen

To create a new project, select File → New → Project....

Then, select D application and click Go.
codeblocks_new_proj
Select D application to create a D project

The new D project wizard will be shown.

codeblocks_create_project_0
Nothing to see here, press Next

codeblocks_create_project_1
Set the project’s name and location in this screen

codeblocks_create_project_2
Finally, choose the compiler you wish. I only have DMD installed at this time, so I chose that one.

That’s it! Your new project is ready.

2. Using the IDE

Code::Blocks does not offer much as an IDE. No code completion, no refactorings, no dub support. What is does have, is debugging support within the IDE. It’s a bit ugly, but it works.

To set a breakpoint, click in the space between the line number and the editor.
codeblocks_set_breakpoing
Click just after the line number to set a breakpoint

To start the debugger, select Debug → Start / Continue

At the bottom of the window, you can type debugger commands. On my system, the default debugger is GDB.
codeblocks_debugging
You can execute debugger commands within the IDE

Install DCD auto-completion engine

The D Completion Daemon is an auto-complete program for the D programming language.

It is used by several IDEs and text editors to provide syntax auto completion (IntelliJ, Atom, …).

How to install

Ubuntu (and other Debian-based distributions)

Open a terminal and execute:

OS-X with Homebrew

Open a terminal and execute:

Windows / Manual Installation

If you are on Windows or want to compile DCD yourself, the easiest way to install it is by using dub. If you haven’t installed dub yet (highly recommended!), read the installation instructions here.

Open a terminal and execute:

Using D with IntelliJ

Before continuing, make sure you have installed:

  1. A D compiler (DMD, GDC, LDC)
  2. dub (D’s package manager) [optional, but highly recommended]
  3. Java
  4. IntelliJ

1. Open IntelliJ

New IntelliJ user

If you haven’t opened IntelliJ before, you will be presented with the Welcome screen.
To directly install the plugin from here, click on the gear icon, like the in the image below.

intellij_15_welcome_screen
IntelliJ 15 welcome screen

Existing IntelliJ user

If you already are an IntelliJ user, your last opened project will be shown. To go into plugins, select File → Settings, and then choose Plugins from the menu on the left.

2. Install the D plugin

From the plugins window (below), select Browse repositories...

intellij_plugins
Plugins window

Install

In the window that opens, type something like D lang.
Select the one whose vendor is MasterThought and click Install.

intellij_dlang_plugin

When it’s finished, you will be asked to restart the IDE. Press Restart.
intellij_restart_prompt

3. Create a D project

To create a project, select Create New Project from the Welcome screen.
intellij_create_project_0

Otherwise, if IntelliJ takes you to your last opened project, select File → New → Project...

3.1 Choose Project Type

Choose the project type from the left-side panel. The recommended type is D Application with DUB (this presumes that you have installed dub).

intellij_create_project_1

3.2 Choose Compiler

In this step you can select which D compiler to use. I only have DMD at this time, so I select that one.

intellij_create_project_2

3.3 dub Project Configuration (for dub projects)

In this screen you can configure parameters for dub, e.g. dub’s configuration file format (json (recommended) or SDL).

intellij_create_project_3

Press next to proceed.

3.4 Project Name

Finally, set a name for your project.

intellij_create_project_4

4. Using the IDE

Now that the project has been created, the main editor window is shown.

intellij_project_view

From the project view (on the left), expand the project by clicking on the icon. In the source folder, dub has created app.d, which contains the application’s main() function.
Double-click on app.d to open it in the editor.

intellij_main_editor
app.d, generated by dub

5. Running the project

To run the project, a Run Configuration must be created.
Select Run → Edit Run... → Run DUB

Even though you have dub installed, the plugin will complain that it doesn’t know where to find it. A pop-up will appear at the top right corner of the editor. Press Configure to set the required configuration.

intellij_configure_run_config

The following window will appear. At the top, you must specify dub’s location. On my machine, Auto Find succeeds in finding it. If on yours it does not, press the ... icon to manually locate it. Finally, press OK.

intellij_dub_config

If all went well, the project will run and you will see the following output in the output panel.

6. Setting up auto-completion

6.1 Install DCD auto-completion engine

The plugin uses DCD for auto-completion, so make sure you have installed it. If you haven’t, read the instructions here.

6.2 Configure the plugin

  1. Select File → Settings...
  2. From the menu on the left, select Other Settings → D Tools
  3. At Dcd Server section, locate the dcd-server executable (or press Auto Find to automatically find it). You also need to set up the import paths of the D runtime and standard library, because dcd doesn’t seem to pick them up by default. In the Flags text field, if you are using DMD and Linux, enter -I/usr/include/dmd/druntime/import -I/usr/include/dmd/phobos/ . If you are on windows, you need to find the corresponding directories in the DMD installation path.
  4. At Dcd Client section, locate the dcd-client executable, just the above.
  5. Press OK

Associative arrays

D has built-in support for associate arrays (AAs) (also known as hash tables).

This is analogous to Java’s Map and C++’s std::unordered_map.

Declaring an associative array

To declare an associative array, use the following syntax:

Inserting into an associative array

To insert into an AA, use the [] operator.
Here is an example that creates an AA with the squares of numbers from [0, 10] and then displays them to standard output.

Running the above displays:
[0:0, 6:36, 7:49, 2:4, 3:9, 10:100, 1:1, 8:64, 5:25, 4:16, 9:81]
Note that the numbers are not sorted — This is expected, as associative arrays are not internally sorted.

Notes

  • Re-assigning an already existing key will replace the previous value.
  • Attempting to access a key that does not exist, will result in a core.exception.RangeError error.

Removing from an associative array

Use the remove() function to remove a key.
aa.remove("hello");

Testing if a key exists

To test if a key exists, use the in operator, which returns a pointer to the value. If the key doesn’t exist, the pointer is null.

Clearing an associative array

There are two ways to clear an associative array:

  1. Iterate over the keys and remove them
  2. Throw away the old AA and create a new one

Method 1 – Remove the keys

Method 2 – Create a new associative array

To throw away the current AA, assign null to it.

Properties

We have already seen some properties of associative arrays, like remove(), and keys. Here are the rest of them:

Property Description
.sizeof Returns the size of the reference to the associative array; it is 4 in 32-bit builds and 8 on 64-bit builds.
.length Returns number of values in the associative array. Unlike for dynamic arrays, it is read-only.
.dup Create a new associative array of the same size and copy the contents of the associative array into it.
.keys Returns dynamic array, the elements of which are the keys in the associative array.
.values Returns dynamic array, the elements of which are the values in the associative array.
.rehash Reorganizes the associative array in place so that lookups are more efficient. rehash is effective when, for example, the program is done loading up a symbol table and now needs fast lookups in it. Returns a reference to the reorganized array.
.byKey() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over the keys of the associative array.
.byValue() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over the values of the associative array.
.byKeyValue() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over key-value pairs of the associative array. The returned pairs are represented by an opaque type with .key and .value properties for accessing the key and value of the pair, respectively.
.get(Key key, lazy Value defVal) Looks up key; if it exists returns corresponding value else evaluates and returns defVal.

Further reading

For more information (e.g. how to use struct or class within associative arrays), read more here [dlang.org].