the d programming language

41
The D Programming The D Programming Language Language CS 4450

Upload: iamrhai2x

Post on 16-Nov-2015

31 views

Category:

Documents


3 download

DESCRIPTION

Presentation for D Programming Language

TRANSCRIPT

  • The D Programming LanguageCS 4450

  • What is D?A system programming language conceived in 1996 as a C++ replacementCombines the power of C++ with the usability of languages like Java, C# and PythonIf something works in C++, C# or Java, it probably works pretty much the same in D

  • Often, programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++. The frequency of this approach indicates that there is a large unmet need in the programming language department.

    D intends to fill that need. It combines the ability to do low-level manipulation of the machine with the latest technologies in building reliable, maintainable, portable, high-level code. D has moved well ahead of any other language in its abilities to support and integrate multiple paradigms like imperative, OOP, and generic programming.

    Walter Bright, Co-designer of D

  • D is like C++It supports static local variablesIt supports pointers

    but you almost never need them (we wont)It has templates

    but theyre easier and more flexible than in C++it supports compile-time executionIts fast

    compiles to native codeIt supports operator overloading

  • D is Like Java and C#It supports garbage collection

    the default, but you can opt-outIt supports explicit, user-defined interfaces

    uses : for inheritance, like C#It has inner classes, like Java and C#It has delegates

    but more powerful than in C#It distinguishes reference and value typesIt supports properties

  • D is Like Python and FP LanguagesIt has modules and packages

    like PythonsIt supports nested functions and closures

    including anonymous functionsIt has a pure FP subset

    and the usual higher-order functionsreduce, map, filter, composesupports lazy evaluation of function parameters

  • D is for Real ProgrammersIt has software engineering support:

    unit testingcontract programminginvariants, preconditions, postconditionsbuilt-in constructs for exception-safe transactions Supports concurrent programming

    with thread-local storageboth lock and lockless programmingCan interoperate with C and C++

  • D'iving In// hello.dimport 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.dChuck-Allisons-iMac-4:CCSC chuck$ ./helloHello, Modern WorldChuck-Allisons-iMac-4:CCSC chuck$ ./hello John JaneHello JohnHello Jane

  • Primitive Data TypesTypeMeaningDefault (.init)void no value n/abool Boolean value falsebyte signed 8 bits 0ubyte unsigned 8 bits 0short signed 16 bits 0ushort unsigned 16 bits 0int signed 32 bits 0uint unsigned 32 bits 0long signed 64 bits 0ulong unsigned 64 bits 0float 32-bit oating-point float. nandouble 64-bit oating-point double. nanreal largest in hardware real. nanchar unsigned 8-bit UTF-8 0xFFwchar unsigned 16-bit UTF-16 0xFFFFdchar unsigned 32-bit UTF-32 0x0000FFFF

    Source: Alexandrescu. All variables get initialized.*

  • Literals and Type InferenceUses the same suffixes as C/C++

    f, l, u, etc.Uses the same prefixes

    0123, 0x1CFAdds binary: 0b01001Type inference with auto:

    auto targetSalary = 15_000_000;auto sq = (double x) { return x*x; };

    sq is a double. Make sure they dont confuse (double x) for a cast, since its a parameter list.*

  • ArraysStatic arrays

    Similar to arrays in C/C++int a[3];int[3] a;// Same thingDynamic Arrays

    Similar to vectors, ArrayLists, etc.See staticarray.dInitialization:

    int[3] a = [1:2, 3];// [0,2,3]

  • Selected Array OperationsIndexing with [ ]Concatenation with ~Copying with a.dupAssignment with =Length with .length

    $ = .length in index expressions (a[2..$])m..n is an array slice (excludes n)can assign to .length to resize dynamic arraysArray-wise operations (mathematical vector operations)See mergesort.d, vector.d

  • StringsStrings are dynamic arrays of immutable characters

    alias immutable(char)[] string;string char(UTF-8)wstring wchar(UTF-16)dstring dchar(UTF-32)foreach visits each character:

    foreach (c; s)

  • Associative ArraysA map (aka dictionary, hash, etc.)

    Except its a built-in type (like dict in Python)Uses 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 */

    remove method*

  • Word Count Program Output/* Abbreviated output from the Gettysburg Address:But,: 1Four: 1who: 3will: 1work: 1world: 1years: 1*/

  • A Word Count Programimport std.stdio, std.array, std.file;

    void wc(string filename) { string[] words = split(cast(string) read(filename)); int[string] counts; foreach (word; words) ++counts[word]; foreach (w; counts.keys.sort) writefln("%s: %d", w, counts[w]);}

    void main(string[] args) { if (args.length == 2) wc(args[1]);}

  • TuplesDefined in std.typeconsTuple!(type1,type2,..)Helper function:

    tuple(val1,val2,)Can use 0-based position numbersCan also use field namesSee tuple.d

  • Data QualifiersFor function parameters:

    in | out | inoutreflazyGeneral declaration qualifiers:

    constimmutable

  • Lazy Parametersimport 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*/

  • ClosuresNested functions are returned as (dynamic) closures

    aka delegates (a code-environment pair)The referencing environment could be a function, class, or objectEscaped activation records are moved from the stack to the garbage-collected heapPlain function pointers also supported:

    int function(int) f; (vs. int (*f)(int); in C)*

  • Plain Function Pointersint f1(int n) {return n+1;}int f2(int n) {return n+2;}int f3(int n) {return n+3;}

    void main() { auto funs = [&f1, &f2, &f3]; foreach (f; funs) writeln(f(1));}

    /* Output:234*/

  • Higher-Level Functions and Closuresimport std.stdio;

    bool delegate(int) gtn(int n) { bool execute(int m) { return m > n; } return &execute;}

    void main() { auto g5 = gtn(5); // Returns a ">5" delegate writeln(g5(1)); // false writeln(g5(6)); // true}

  • Lambda Expressionsauto gtn(int n) { return delegate bool(int m) {return m > n;};}

    // Preferred:auto gtn(int n) { return (int m) {return m > n;};}

    Note auto return type.*

  • An Object Calling Environmentvoid main() { class A { int fun() { return 42; } } A a = new A; auto dg = &a.fun; // A bound method writeln(dg()); // 42}

  • Parametric PolymorphismCompile-time Parametersauto 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 Constraintsimport 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"); // Error writeln(g5s("bar")); writeln(g5s("foo"));}

    Compile-time error.*

  • Compile-Time Function Evaluation// ctfe.d: Compile-time function executionimport std.stdio, std.conv;int square(int i) { return i * i; }void main() { static int n = square(3); // compile time writefln(text(n)); writefln(text(square(4))); // runtime}

  • Pure FunctionsFor Referential Transparencyimport std.stdio;import std.conv;

    pure ulong fib(uint n) { if (n == 0 || n == 1) return n; ulong a = 1, b = 1; foreach (i; 2..n) { auto t = b; b += a; a = t; } return b;}

    void main(string[] args) { if (args.length > 1) writeln(fib(to!(uint)(args[1])));}

    Local mutables okay.*

  • Selected Higher-Level Functionsint[] nums = [1,2,3,4,5];

    auto squarelist(int[] list) { // 1 4 9 16 25 return map!("a*a")(list);}

    auto sqsum2(int[] list) {// 55 return reduce!("a + b*b")(0,list); // (list)}

    auto evens(int[] list) {// 2 4 return filter!("a % 2 == 0")(list);}

    alias compose!("a/3.0","a*a","a+1.0") comp;writeln(comp(2.0));// 3 = (2+1)^2 / 3alias pipe!(div3,sq,pls1) pip; // fns in scopewriteln(pip(2.0));// 1.4444 = (2/3)^2+1

    This slide shows string versions of the passed function. They can be any functional form (function, delegate, functor, string). See compose.d.*

  • std.functional.memoizeMemoization 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 ListsUse in the parameter definitionHomogeneous

    use in the runtime parameter listpasses a dynamic arrayHeterogeneous

    use in the compile-time parameter listpasses a tupleSee varargs.d

  • An Unsafe Functionvoid g() { risky_op1();// May acquire resources risky_op2(); risky_op3(); writeln("g succeeded");}Assume further that these functions must run to completion or not at all.

  • An Unsavory Solutionvoid 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 StatementOptions: exit | success | failurevoid g() { risky_op1(); scope(failure) undo_risky_op1(); risky_op2(); scope(failure) undo_risky_op2(); risky_op3(); writeln("g succeeded");}

  • Contract Programming ExampleAlso illustrates value types, operator overloading, and unit testingimport std.math; // For abs()

    struct Rational { private int num = 0; private int den = 1; // Local helper function static int gcd(int m, int n) { return n == 0 ? abs(m) : gcd(abs(n), abs(m)%abs(n)); } // Class invariant invariant() { assert(den > 0 && gcd(num, 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) Rational opBinary(string op)(Rational r) if (op == "+) { return Rational(num*r.den + den*r.num, den*r.den); }}

    unittest {// Compile with unittest option auto r1 = Rational(1,2), r2 = Rational(3,4), r3 = r1 + r2; assert(r3.num == 5 && r3.den == 4);}

    void main(){}

  • Variant TypesSometimes you want to hold any type

    hopefully not too often!Variant can hold any typeYou can only get out what was last put in

    see variant.dBounded, discriminated unions with Algebraic

    like ML unionssee variant2.d

  • ThreadsA thread is a path of execution inside a running program (process)

    A thread is launched on a function basis

    Using the std.concurrency.spawn

    See concur1.d

  • Message PassingA paradigm for thread-to-thread communication

    Threads send/receive data to/from each other

    Less problematic (and less flexible) than sharing data via mutexesSee concur1,2,3.d

  • Infinite Streams in DThreads and Message Passing naturally support streams

    See stream.d

    Source: Alexandrescu. All variables get initialized.*sq is a double. Make sure they dont confuse (double x) for a cast, since its a parameter list.*remove method*Note auto return type.*Compile-time error.*Local mutables okay.*This slide shows string versions of the passed function. They can be any functional form (function, delegate, functor, string). See compose.d.*