September 2016

Updated: December 2018

Meta

  • Ask questions, interrupt, terms you don't know
  • Info you have to dig for
  • Not a D expert, just used it for years

The Language Zoo

  • Scripting - Python, Ruby, Javascript, Perl, usually interpreted
  • Application - Java, C#, Go, use Virtual Machine and Garbage Collection, often with Just-In-Time compilation
  • Systems programming - C, C++, Rust, Swift, Obj-C, use Ahead-of-Time compilation
  • Special purpose - R, Erlang, Julia, Dart
  • D - one language to rule them all, AoT-compiled, GC that can be turned off, very fast compilation

Where to first?

import std.stdio, std.algorithm;

void main()
{
    string inputLine;
    string[] tempArray;

    while((inputLine = stdin.readln()) !is null)
        tempArray ~= inputLine;

    sort!((a, b) => a > b)(tempArray); // descending order

    foreach(line; tempArray)
        write(line);
}

Classic programming

// Sort lines
import std.stdio, std.array, std.algorithm;

void main()
{
    stdin
        .byLineCopy
        .array
        .sort!((a, b) => a > b) // descending order
        .each!writeln;
}

D component programming

D aims to be a useful programming language... D's job is to get s*** done quickly and effiently and make money for the businesses that use it.
- D forum, July 2016

Walter Bright

Creator

  • Leading expert at implementing compilers: C, C++, Java, Javascript
  • Started by writing one of the first computer strategy games, Empire, in college
  • Wrote the first C++ compiler that didn't translate to C first
  • Retired in 1999, then started D
  • Focus on compile and runtime speed, along with clean source

Andrei Alexandrescu
Co-architect

  • 2001: Wrote Modern C++ Design
  • 2006: Joined D as co-architect, began version 2.0
  • 2010: Wrote the reference book on D 2.0
  • 2015: Left Facebook to start the D Foundation
  • Design patterns: programmer “expands” mental macros - Total plasticity, no code reuse
  • Policy-based design: programmer assembles rigid macros - No plasticity, good code reuse
  • DbI: programmer molds macros that communicate with, and adapt to, one another - Good plasticity, good code reuse

How do the co-architects shape the language?

  • D1 was simpler, while D2 has a lot more features, like C++
  • Community-driven language, with Walter and Andrei as gatekeepers for the compiler and standard library
  • No major corporate sponsor, which slows development
  • Humble outlook on new features

High-level overview

  • In use at WekaIO, Sociomantic, Remedy Games, eBay, Facebook
  • Compiled Ahead-Of-Time like Swift, Rust, and Go, plus mobile Java and C#
  • Reference compiler, dmd, very fast with small 60 kloc backend: scripting
  • DMD frontend written in D tied to LLVM and GCC backends: LDC and GDC
  • Garbage-collected, though @nogc usage is increasing
  • Available on Windows, macOS, linux/BSD, and Android; iOS mostly done but on hold

Procedural

struct Boo { int x; };

void foo(struct Boo* boo)
{
    if(boo != NULL)
    {
        boo->x = 3;
    }
}
struct Boo { int x; }

void foo(Boo* boo)
{
    if(boo !is null)
    {
        boo.x = 3;
    }
}

C

D

Object-Oriented

interface Cat { void roar(); }

class Tiger : Cat
{
public:
    void roar(){ privateRoar();}
private:
     void privateRoar(){ writeln("Roar!"); }
}

void main()
{
    Tiger tony = new Tiger();
    tony.roar();
}

Functional

immutable int x = 3;
x = 5; // error

int square(int x) pure
{
    return x * x;
}

int[] data = [1, 2, 3, 4, 5];
auto oddData = data.filter!(x => x%2 == 1);

Features

Modules

module danatic.list;

import std.array : array;

struct List
{
    import std.process : spawnShell;

    void print()
    {
        import std.stdio : write;
        // implement print
    }
}

Component programming

Templates

class List(T, U)
{
    List add(T t, U u)
    {
        //do the add
    }
}

void main()
{
    auto list = new List!(int, string);
    list.add(3, "foo");
}

Slices

int[5] a = [1, 2, 3, 4, 5];

int[] b = a[2..$];

b[0] = 1;

Concurrency

Compile-Time Function Execution (CTFE)

int square(int i)
{
    return i * i;
}

enum x = square(3);

int y = square(5);

Exceptions

try { dangerousCode();}
catch(Exception e) { //process the exception }
finally { //always run this code }

void safeTransaction(Transaction foo) nothrow
{
    atomicTransaction(foo);
    scope(failure) rollbackTransaction(foo);

    process(foo);
    scope(exit) unprocess(foo);

    saveToDisk(foo);
    scope(success) writefln("exited with a safe transaction");
}

Unit tests and Contracts

class Squareroot
{
    long run(long x)
    in { assert(x >= 0); }
    out (result)
    {
        assert((result * result) <= x && (result+1) * (result+1) > x);
    }
    body
    {
        return cast(long)std.math.sqrt(cast(real)x);
    }
    
    unittest
    {
        Squareroot sqroot = new Squareroot;
        assert(sqroot.run(3) == std.math.sqrt(3);
        assert(sqroot.run(-2) == std.math.sqrt(-2)); //error
    }
}

Mixins

int a = 5;

mixin("int b = a;"); // b exists and is now 5

Current usage and successes

Regular Expressions

Sociomantic

vibe.d web framework

D front-end

  • 60+ kloc of D, ported automatically from simple C++
  • Linked against 3 C++ backends: dmc, gcc, llvm
  • Slowly refactored to idiomatic D

Facebook

C++ following

Try it out!

Made with Slides.com