Professional Documents
Culture Documents
Language
CS 4450
What is D?
A
D is like C++
It
has templates
fast
It
like Pythons
It
It
unit testing
contract programming
invariants, preconditions, postconditions
concurrent programming
D'iving In
// hello.d
import std.stdio;
void main(string[] args) {
if (args.length > 1)
foreach (a; args[1..$])
writeln("Hello " ~ a);
else
writeln("Hello, Modern World");
}
Chuck-Allisons-iMac-4:CCSC chuck$ dmd hello.d
Chuck-Allisons-iMac-4:CCSC chuck$ ./hello
Hello, Modern World
Chuck-Allisons-iMac-4:CCSC chuck$ ./hello John Jane
Hello John
Hello Jane
Default (.init)
n/a
false
0
0
0
0
0
0
0
0
float. nan
double. nan
real. nan
0xFF
0xFFFF
f, l, u, etc.
Uses
0123, 0x1CF
Adds binary: 0b01001
Type
Arrays
Static
arrays
Arrays
staticarray.d
Initialization:
int[3] a = [1:2, 3];
// [0,2,3]
Array-wise
operations (mathematical
vector operations)
See mergesort.d, vector.d
Strings
Strings
string char
wstring wchar
dstring dchar
foreach
(UTF-8)
(UTF-16)
(UTF-32)
foreach (c; s)
Associative Arrays
A
array syntax:
void main() {
int[string] mymap = ["one":1, "two":2];
mymap["three"] = 3;
foreach (k,v; mymap) writef("%s:%s ",k,v);
}
/* Output:
three:3 one:1 two:2
*/
who: 3
will: 1
work: 1
world: 1
years: 1
*/
Tuples
Defined
in std.typecons
Tuple!(type1,type2,..)
Helper
function:
tuple(val1,val2,)
Can use 0-based position
numbers
Can also use field names
See tuple.d
Data Qualifiers
For
function parameters:
in | out | inout
ref
lazy
General
declaration qualifiers:
const
immutable
Lazy Parameters
import std.stdio;
void f(bool b, lazy void g) {
if (b) {
g();
}
}
void main() {
f(false, writeln("executing g"));
f(true, writeln("executing g"));
}
/* Output:
executing g
*/
Closures
Nested
Plain
20
/* Output:
2
3
4
*/
Lambda Expressions
auto gtn(int n) {
return delegate bool(int m) {return m > n;};
}
// Preferred:
auto gtn(int n) {
return (int m) {return m > n;};
}
An Object Calling
Environment
void main() {
class A { int fun() { return 42; } }
A a = new A;
auto dg = &a.fun; // A bound method
writeln(dg());
// 42
}
Parametric Polymorphism
Compile-time Parameters
auto gtn(T)(T n) {
return (T m) {return m > n;};
}
void main() {
auto g5 = gtn(5);
writeln(g5(1));
writeln(g5(6));
auto g5s = gtn("baz");
writeln(g5s("bar"));
writeln(g5s("foo"));
}
Compile-Time Constraints
import std.stdio, std.traits;
auto gtn(T)(T n) if (isNumeric!T) {
return (T m) {return m > n;};
}
void main() {
auto g5 = gtn!int(5);
writeln(g5(1));
writeln(g5(6));
auto g5s = gtn!string("baz");
writeln(g5s("bar"));
writeln(g5s("foo"));
}
// Error
Compile-Time Function
Evaluation
// ctfe.d: Compile-time function execution
import std.stdio, std.conv;
void main() {
static int n = square(3); // compile time
writefln(text(n));
writefln(text(square(4)));
// runtime
}
Pure Functions
Selected Higher-Level
Functions
int[] nums = [1,2,3,4,5];
// 1 4 9 16 25
std.functional.memoize
Memoization
is a technique
where a cache of input-output
pairs is kept for function calls
Avoids
recomputing function
values
See
fib.d
Variable-length Argument
Lists
varargs.d
An Unsafe Function
void g() {
risky_op1(); // May acquire resources
risky_op2();
risky_op3();
writeln("g succeeded");
}
An Unsavory Solution
void g() {
risky_op1();
try {
risky_op2();
}
catch (Exception x) {
undo_risky_op1(); // Back-out op1
throw x;
// Rethrow exception
}
try {
risky_op3();
writeln("g succeeded");
}
catch (Exception x) {
// Back-out op1 and op2 in reverse order
undo_risky_op2();
undo_risky_op1();
throw x;
}
}
Ds scope Statement
Options: exit | success | failure
void g() {
risky_op1();
scope(failure) undo_risky_op1();
risky_op2();
scope(failure) undo_risky_op2();
risky_op3();
writeln("g succeeded");
}
Contract Programming
Example
Also illustrates
operator
import
std.math;value
//types,
For abs()
and unit testing
overloading,
struct Rational {
private int num = 0;
private int den = 1;
Contract Programming
Example
(continued)
// Constructor
this(int n, int d = 1)
// Constructor precondition
in {
assert(d != 0);
}
body {
num = n;
den = d;
auto div = gcd(num, den);
if (den < 0)
div = -div;
num /= div;
den /= div;
}
Contract Programming
Example
(concluded)
// ("if" form evaluated at compile time)
Variant Types
Sometimes
Threads
A
Using
See
the std.concurrency.spawn
concur1.d
Message Passing
A paradigm for thread-to-thread
communication
Threads send/receive data to/from
each other
Less problematic (and less flexible)
than sharing data via mutexes
See concur1,2,3.d
Infinite Streams in D
Threads
See
stream.d