Вы находитесь на странице: 1из 20

Dlang

Tolstokulakov Nikolay

Going to native
1. 2. 3. 4. C++11 GoLang Rust DLang

Authors
Andrei Alexandrescu
Modern C++ Design: Generic Programming and Design Patterns Applied C++ Coding Standards: 101 Rules, Guidelines, and Best Practices. The D Programming Language.

Walter Bright
C and C++ compilers: Digital Mars C++ Symantec C++ Zortech C++ (the first native C++ compiler)

Dlang.org
D is a language with C-like syntax and static typing. It pragmatically combines efficiency, control, and modeling power, with safety and programmer productivity. Convenience - minimal boilerplate, automatic memory management,
slices, ranges

Power - polymorphism, value semantics, functional style, generics,


generative programming, contract programming, ... Innovative approach to concurrency

Efficiency - native code, native pointers, type casts, access to any C


function, and even inline assembly code

TIOBE Programming Community Index for October 2013


21 22 23 24 25 26 27 28 29 30 R 0.553% 31 32 33 34 35 36 37 38 39 40 ABAP C shell 0.394% 0.382% 0.380% 41 42 43 44 45 46 47 48 49 50 JavaFX Script Tcl 0.294% 0.292% 0.297% SAS 0.543% Ada 0.510% F# 0.499% 0.474%

Common Lisp NXT-G Scheme

Erlang

0.366% 0.360%

Max/MSP 0.276% Scratch Haskell ML 0.270% 0.245%

Fortran

Assembly 0.471% Bash 0.470% Ladder Logic 0.457% Logo 0.433% Lua 0.413%

Scala0.345% D 0.337% 0.328% 0.319%

0.245%

Prolog

PL/I 0.240% ActionScript Emacs Lisp 0.215% 0.210%

RPG (OS/400)

PostScript 0.312%

DLang
It is complex (more 100 keywords vs 50 in Java) Syntactic sugar & features
slices, auto, powerful foreach, final switch, ... unit tests, contract programming compile time intersection lamda, delegate scope ranges anti-hijacking

Powerful metaprogramming

The sope statement (Java, C#)


(action) try{ (next) }catch(Exception e){ (rollback) throw e }finally{ (cleanup) }

The scope statement


(actionN) scope(failure) (rollbackN) scope(exit) (cleanupN) (actionN+1) scope(failure) (rollbackN+1) scope(exit) (cleanupN+1)

Built-in array
bool binarySearch(T)(T[] input, T value) { if (input.empty) return false; auto i = input.length / 2; auto mid = input[i]; if (mid > value) return binarySearch(input[0 .. i]); if (mid < value) return binarySearch(input[i + 1 .. $]); return true; }

Ranges
InputRange:
front - get the first element of the range popFront - remove the first element of the range empty - are there more elements in the range? InputRange ForwardRange BidirectionalRange RandomAccessRange (infinite) RandomAccessRange (finite)

Ranges - sort input stream #1


import std.array, std.algorithm, std.stdio; void main(){ auto lines = stdin.byLine(KeepTerminator.yes); auto dupLines = map!(a => a.idup)(lines); auto arrLines = array(dupLines); sort(arrLines); copy(arrLines, stdout.lockingTextWriter()); }

Sort input stream #2 (UFCS)


#!/usr/bin/rdmd import std.array, std.algorithm, std.stdio; void main(){ stdin.byLine(KeepTerminator.yes) .map!(a => a.idup) .array .sort .copy(stdout.lockingTextWriter()); }

Voldemort
http://habrahabr.ru/post/183488/ auto generator(uint seed) { struct RandomNumberGenerator { @property int front() { return ((seed / 0x10000) * seed) >> 16; } void popFront() { seed = seed * 1103515245 + 12345; @property bool empty() { return false; } } RandomNumberGenerator g; g.popFront(); return g; } // get it going }

Compile time functions - C++


template <int N> struct Factorial { enum { value = N * Factorial<N - 1>::value }; }; template <> struct Factorial<0> { enum { value = 1 }; }; void foo() { int x = Factorial<0>::value; // == 1 int y = Factorial<4>::value; // == 24 }

Compile time - DLang


import std.stdio, std.bigint; BigInt factorial(uint n){ BigInt rs = 1; foreach(i; 1..n+1) rs *= i; return rs; } enum f3 = factorial(20); void main(){ writefln("%s - %s", f3, factorial(4)); }

Pegged
https://github.com/PhilippeSigaud/Pegged/ mixin(grammar(` Arithmetic: Term Add Sub Mul Div `)); < Factor (Add / Sub)* < "+" Factor < "-" Factor < "*" Primary < "/" Primary

Factor < Primary (Mul / Div)*

..

float interpreter(string expr){ auto p = Arithmetic(expr); float value(ParseTree p) { switch (p.name) { case "Arithmetic": return value(p.children[0]); case "Arithmetic.Term": . void main(string args[]){ pragma(msg, interpreter("1 + 2 - (3-5)*6")); foreach(a; args[1 .. $]) writefln("%s = %s", a, interpreter(a)); }

Dynamic typing
http://wiki.dlang.org/Dynamic_typing

var fromArgs = var.fromJson(args[1]); writefln("%s", fromArgs.keys); uint ids[] = cast(uint[])fromArgs.keys; var newIds = ids.map!(a=>a*2).array; fromArgs.keys = newIds; writefln("%s", fromArgs); string strIds[] = cast(string[])fromArgs.keys; var newStrIds = strIds.map!(a=>a~"!").array; fromArgs.keys = newStrIds; writefln("%s", fromArgs); ./jsvar_test '{"keys":[1,2,3]}' [1, 2, 3] {"keys":[2,4,6]} {"keys":["2!","4!","6!"]}

Compilers, IDE, tools,


http://wiki.dlang.org/The_D_Programming_Language Compilers: dmd, gdc, ldc IDE DDT - Eclipse based Mono-D Visual-D

Materials
dlang.org dconf.org The D Programming Language (Andrei Alexandrescu) Three Unlikely Successful Features of D

Вам также может понравиться