Dlang
Tolstokulakov Nikolay
Going to native
1. C++11
2. GoLang
3. Rust
4. 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 R 0.553% 31 ABAP 0.394% 41 JavaFX Script 0.297%
22 SAS 0.543% 32 C shell 0.382% 42 Tcl 0.294%
23 Ada 0.510% 33 Common Lisp 0.380% 43 Erlang 0.292%
24 F# 0.499% 34 NXT-G 0.366% 44 Max/MSP 0.276%
25 Fortran 0.474% 35 Scheme 0.360% 45 Scratch 0.270%
26 Assembly 0.471% 36 Scala0.345% 46 Haskell 0.245%
27 Bash 0.470% 37 D 0.337% 47 ML 0.245%
28 Ladder Logic 0.457% 38 Prolog 0.328% 48 PL/I 0.240%
29 Logo 0.433% 39 RPG (OS/400) 0.319% 49 ActionScript 0.215%
30 Lua 0.413% 40 PostScript 0.312% 50 Emacs Lisp 0.210%
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(); // get it going
return g;
}
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 < Factor (Add / Sub)*
Add < "+" Factor
Sub < "-" Factor
Factor < Primary (Mul / Div)*
Mul < "*" Primary
Div < "/" Primary
……………..
`));
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

Tech Talks @NSU: DLang: возможности языка и его применение

  • 1.
  • 2.
    Going to native 1.C++11 2. GoLang 3. Rust 4. DLang
  • 3.
    Authors Andrei Alexandrescu ● ModernC++ 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)
  • 4.
    Dlang.org D is alanguage 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
  • 5.
    TIOBE Programming CommunityIndex for October 2013 21 R 0.553% 31 ABAP 0.394% 41 JavaFX Script 0.297% 22 SAS 0.543% 32 C shell 0.382% 42 Tcl 0.294% 23 Ada 0.510% 33 Common Lisp 0.380% 43 Erlang 0.292% 24 F# 0.499% 34 NXT-G 0.366% 44 Max/MSP 0.276% 25 Fortran 0.474% 35 Scheme 0.360% 45 Scratch 0.270% 26 Assembly 0.471% 36 Scala0.345% 46 Haskell 0.245% 27 Bash 0.470% 37 D 0.337% 47 ML 0.245% 28 Ladder Logic 0.457% 38 Prolog 0.328% 48 PL/I 0.240% 29 Logo 0.433% 39 RPG (OS/400) 0.319% 49 ActionScript 0.215% 30 Lua 0.413% 40 PostScript 0.312% 50 Emacs Lisp 0.210%
  • 6.
    DLang ● It iscomplex (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
  • 7.
    The sope statement(Java, C#) (action) try{ (next) }catch(Exception e){ (rollback) throw e }finally{ (cleanup) }
  • 8.
    The scope statement (actionN) scope(failure)(rollbackN) scope(exit) (cleanupN) (actionN+1) scope(failure) (rollbackN+1) scope(exit) (cleanupN+1)
  • 9.
    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; }
  • 10.
    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)
  • 11.
    Ranges - sortinput 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()); }
  • 12.
    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()); }
  • 13.
    Voldemort типы http://habrahabr.ru/post/183488/ auto generator(uintseed) { 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(); // get it going return g; }
  • 14.
    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 }
  • 15.
    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)); }
  • 16.
    Pegged https://github.com/PhilippeSigaud/Pegged/ mixin(grammar(` Arithmetic: Term < Factor(Add / Sub)* Add < "+" Factor Sub < "-" Factor Factor < Primary (Mul / Div)* Mul < "*" Primary Div < "/" Primary …………….. `));
  • 17.
    float interpreter(string expr){ autop = 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)); }
  • 18.
    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!"]}
  • 19.
    Compilers, IDE, tools,… http://wiki.dlang.org/The_D_Programming_Language ● Compilers: dmd, gdc, ldc ● IDE ○ DDT - Eclipse based ○ Mono-D ○ Visual-D
  • 20.
    Materials ● dlang.org ● dconf.org ●The D Programming Language (Andrei Alexandrescu) ● Three Unlikely Successful Features of D