Why I use the D programming language for scripting | Opensource.com

Why I use the D programming language for scripting

The D programming language is best known as a system programming language, but it's also a great option for scripting.

Business woman on laptop sitting in front of window
Image credits : 

Image by Mapbox Uncharted ERG, CC-BY 3.0 US


Subscribe now

Get the highlights in your inbox every week.

The D programming language is often advertised as a system programming language due to its static typing and metaprogramming capabilities. However, it's also a very productive scripting language.

Python is commonly chosen for scripting due to its flexibility for automating tasks and quickly prototyping ideas. This makes Python very appealing to sysadmins, managers, and developers in general for automating recurring tasks that they might otherwise have to do manually.

It is reasonable to expect any other script-writing language to have these Python traits and capabilities. Here are two reasons why I believe D is a good option.

1. D is easy to read and write

As a C-like language, D should be familiar to most programmers. Anyone who uses JavaScript, Java, PHP, or Python will know their way around D.

If you don't already have D installed, install a D compiler so that you can run the D code in this article. You may also use the online D editor.

Here is an example of D code that reads words from a file named words.txt and prints them on the command line:


Write the script in D:

#!/usr/bin/env rdmd
// file print_words.d

// import the D standard library
import std;

void main(){
    // open the file

         //iterate by line

        // print each number

This code is prefixed with a shebang that will run the code using rdmd, a tool that comes with the D compiler to compile and run code. Assuming you are running Unix or Linux, before you can run this script, you must make it executable by using the chmod command:

chmod u+x print_words.d

Now that the script is executable, you can run it:


This should print the following on your command line:


Congratulations! You've written your first D script. You can see how D enables you to chain functions in sequence to make reading the code feel natural, similar to how you think about problems in your mind. This feature makes D my favorite programming language.

Try writing another script: A nonprofit manager has a text file of donations with each amount on separate lines. The manager wants to sum the first 10 donations and print the amounts:

#!/usr/bin/env rdmd
// file sum_donations.d

import std;

void main()
    double total = 0;

    // open the file

         // iterate by line

         // pick first 10 lines

        // remove new line characters (\n)

         // convert each to double

        // add element to total
        .tee!((x) { total += x; })

        // print each number

    // print total
    writeln("total: ", total);

The ! operator used with each is the syntax of a template argument.

2. D is great for quick prototyping

D is flexible for hammering code together really quickly and making it work. Its standard library is rich with utility functions for performing common tasks, such as manipulating data (JSON, CSV, text, etc.). It also comes with a rich set of generic algorithms for iterating, searching, comparing, and mutating data. These cleverly crafted algorithms are oriented towards processing sequences by defining generic range-based interfaces.

The script above shows how chaining functions in D provides a gist of sequential processing and manipulating data. Another appeal of D is its growing ecosystem of third-party packages for performing common tasks. An example is how easy it is to build a simple web server using the Vibe.d web framework. Here's an example:

#!/usr/bin/env dub
/+ dub.sdl:
dependency "vibe-d" version="~>0.8.0"

void main()
    import vibe.d;
    listenHTTP(":8080", (req, res) {
        res.writeBody("Hello, World: " ~ req.path);

This uses the official D package manager, Dub, to fetch the vibe.d web framework from the D package repository. Dub takes care of downloading the Vibe.d package, then compiling and spinning up a web server on localhost port 8080.

Give D a try

These are only a couple of reasons why you might want to use D for writing scripts.

D is a great language for development. It's easy to install from the D download page, so download the compiler, take a look at the examples, and experience D for yourself.

Coding on a computer

UFCS gives you the power to compose reusable code that has a natural flow without sacrificing convenience.
Bird nest in nature

Nesting makes it easier for collaborators to understand a codebase. Here is an example of nesting in D to create clean, reusable code.


About the author

Lawrence Aberba - Lawrence is a freelance full stack engineer (React, Node.Js, D) and a 100% Linux user. He loves to design and code. Passionate about SaaS and the modern Web.