c
2012– Andrei Alexandrescu.
1 / 42
Systems Programming & Beyond using C++ and D Three Unlikely Successful Features of D
Andrei Alexandrescu
andrei@erdani.com
Prepared for LASER Summer School 2012
Systems Programming & Beyond using C++ and D Three Unlikely - - PowerPoint PPT Presentation
Systems Programming & Beyond using C++ and D Three Unlikely Successful Features of D Andrei Alexandrescu andrei@erdani.com Prepared for LASER Summer School 2012 1 / 42 2012 Andrei Alexandrescu. c 1. The scope Statement: Casual
c
2012– Andrei Alexandrescu.
1 / 42
Andrei Alexandrescu
Prepared for LASER Summer School 2012
c
2012– Andrei Alexandrescu.
2 / 42
c
2012– Andrei Alexandrescu.
3 / 42
c
2012– Andrei Alexandrescu.
3 / 42
c
2012– Andrei Alexandrescu.
3 / 42
c
2012– Andrei Alexandrescu.
3 / 42
c
2012– Andrei Alexandrescu.
4 / 42
c
2012– Andrei Alexandrescu.
5 / 42
c
2012– Andrei Alexandrescu.
6 / 42
c
2012– Andrei Alexandrescu.
7 / 42
c
2012– Andrei Alexandrescu.
9 / 42
c
2012– Andrei Alexandrescu.
10 / 42
c
2012– Andrei Alexandrescu.
11 / 42
c
2012– Andrei Alexandrescu.
12 / 42
action1 try { action2 try { next2 } catch (Exception e) { rollback2 throw e; } finally { cleanup2 } } catch (Exception e) { rollback1 throw e; } finally { cleanup1 }
c
2012– Andrei Alexandrescu.
13 / 42
c
2012– Andrei Alexandrescu.
14 / 42
c
2012– Andrei Alexandrescu.
15 / 42
c
2012– Andrei Alexandrescu.
16 / 42
c
2012– Andrei Alexandrescu.
17 / 42
c
2012– Andrei Alexandrescu.
18 / 42
c
2012– Andrei Alexandrescu.
19 / 42
c
2012– Andrei Alexandrescu.
20 / 42
import std.exception, std.file, std.stdio; void main(string[] args) { enforce(args.length == 3, "Usage: ..."); scope(success) rename(args[2] ~ ".tmp", args[2]); auto src = File(args[1]), tgt = File(args[2] ~ ".tmp", "w"); scope(failure) remove(args[2] ~ ".tmp"); foreach (chunk; src.byChunk(1024 * 64)) { tgt.rawWrite(chunk); } }
c
2012– Andrei Alexandrescu.
21 / 42
c
2012– Andrei Alexandrescu.
22 / 42
c
2012– Andrei Alexandrescu.
23 / 42
c
2012– Andrei Alexandrescu.
24 / 42
c
2012– Andrei Alexandrescu.
25 / 42
c
2012– Andrei Alexandrescu.
26 / 42
c
2012– Andrei Alexandrescu.
27 / 42
c
2012– Andrei Alexandrescu.
28 / 42
1 5 2 3 6
· · · · · ·
c
2012– Andrei Alexandrescu.
29 / 42
c
2012– Andrei Alexandrescu.
30 / 42
1 5 2 3 6
· · · · · ·
c
2012– Andrei Alexandrescu.
31 / 42
c
2012– Andrei Alexandrescu.
32 / 42
c
2012– Andrei Alexandrescu.
33 / 42
unsigned integrals
results, or doesn’t compile
c
2012– Andrei Alexandrescu.
34 / 42
short a; byte b; uint c; auto d = a * b + c; // what’s the type of d?
c
2012– Andrei Alexandrescu.
35 / 42
computed as above
c
2012– Andrei Alexandrescu.
36 / 42
c
2012– Andrei Alexandrescu.
37 / 42
// This is not D code byte a, b, c; a = (byte) (a | b & c); b = (byte) (b | 1);
c
2012– Andrei Alexandrescu.
38 / 42
intermediate results
byte a, b, c; a = a | b & c; // range -128..127 b = b | 1; // range 0..1
c
2012– Andrei Alexandrescu.
39 / 42
within the target type
c
2012– Andrei Alexandrescu.
40 / 42
byte a, b, c; a = 1; b = c | a; // error
justifiable
c
2012– Andrei Alexandrescu.
41 / 42
c
2012– Andrei Alexandrescu.
42 / 42
c
2012– Andrei Alexandrescu.
42 / 42
c
2012– Andrei Alexandrescu.
42 / 42