D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 •...
Transcript of D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 •...
![Page 1: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/1.jpg)
c© 2014– Andrei Alexandrescu. 1 / 44
D Programming Language: The Sudden
Andrei Alexandrescu, Ph.D.
Research Scientist, Facebook
YOW 2014
![Page 2: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/2.jpg)
Introduction
c© 2014– Andrei Alexandrescu. 2 / 44
![Page 3: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/3.jpg)
Introduction
c© 2014– Andrei Alexandrescu. 3 / 44
![Page 4: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/4.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
![Page 5: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/5.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
![Page 6: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/6.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
![Page 7: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/7.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
![Page 8: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/8.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
Sequel to BoB and Operation Flamethrower
![Page 9: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/9.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
Sequel to BoB and Operation Flamethrower
• Newburn
![Page 10: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/10.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
Sequel to BoB and Operation Flamethrower
• Newburn
Sequel to Chucky and Spontaneous Combustion
![Page 11: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/11.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
Sequel to BoB and Operation Flamethrower
• Newburn
Sequel to Chucky and Spontaneous Combustion
• The Dark Knight Rises
![Page 12: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/12.jpg)
Sudden Sequels
c© 2014– Andrei Alexandrescu. 4 / 44
• Pronouncing “Melbourne” and “Brisbane”
• Burn Identity
Sequel to Bourne Identity and Burn Notice
• Airburn Infantry
Sequel to BoB and Operation Flamethrower
• Newburn
Sequel to Chucky and Spontaneous Combustion
• The Dark Knight Rises
Batman vs. Bun
![Page 13: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/13.jpg)
![Page 14: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/14.jpg)
![Page 15: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/15.jpg)
Motivation
c© 2014– Andrei Alexandrescu. 7 / 44
• Systems-level programming a necessity
• Several application categories
• Faster is better—no “good enough” limit
• Ever-growing modeling needs
• No room below, no escape: all in same language
◦ Runtime support
◦ Machine, device interface
◦ Base library
• This is (literally) where the buck stops
![Page 16: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/16.jpg)
D Design Principles
c© 2014– Andrei Alexandrescu. 8 / 44
• Leave no room below
◦ share memory model with C
◦ statically typed
• Multi-paradigm; balanced
• Practical
• Principled
• Avoid arcana
![Page 17: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/17.jpg)
Why?
c© 2014– Andrei Alexandrescu. 9 / 44
• Party line
◦ convenience
◦ modeling power
◦ efficiency
• Actual reasons
◦ Produces fast binaries, fast
◦ Easier to get into than alternatives
◦ Fun
![Page 18: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/18.jpg)
Why not?
c© 2014– Andrei Alexandrescu. 10 / 44
• Party line
◦
• Actual reasons
◦ Poor on formal specification
◦ Little corporate pickup, support
◦ Dearth of libraries
◦ Large
![Page 19: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/19.jpg)
The “Meh”
c© 2014– Andrei Alexandrescu. 11 / 44
#!/usr/bin/rdmd
import std.stdio;
void main() {
writeln("Hello, world!");
}
• Why the std.std stutter?
• Why import stuff for everything?
• Why no code at top level?
• However:
◦ Simple
◦ Correct
◦ Scriptable
![Page 20: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/20.jpg)
The Innocently Plausible
c© 2014– Andrei Alexandrescu. 12 / 44
void main() {
import std.stdio;
writeln("Hello, world!");
}
• Doesn’t work in Java, C#
• Career-limiting move in Python, C, C++
• In D, most everything can be scoped everywhere
◦ Functions
◦ Types (Voldermort types)
◦ Even generics
◦ Better modularity, reasoning
![Page 21: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/21.jpg)
The Unexpected Emergent
c© 2014– Andrei Alexandrescu. 13 / 44
void log(T)(T stuff) {
import std.datetime, std.stdio;
writeln(Clock.currTime(), ’ ’, stuff);
}
void main() {
log("hello");
}
• If not instantiated, no import
• imports cached once realized
• Generics faster to build, import
• Less pressure on linker
![Page 22: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/22.jpg)
Heck, add variadics too
c© 2014– Andrei Alexandrescu. 14 / 44
void log(T...)(T stuff) {
import std.datetime, std.stdio;
writeln(Clock.currTime(), ’ ’, stuff);
}
void main() {
log("Reached Nirvana level: ", 9);
}
![Page 23: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/23.jpg)
Suddenly
c© 2014– Andrei Alexandrescu. 15 / 44
Natural lexical scoping leads
to faster builds
![Page 24: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/24.jpg)
Approach to Purity
c© 2014– Andrei Alexandrescu. 16 / 44
![Page 25: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/25.jpg)
Thesis
c© 2014– Andrei Alexandrescu. 17 / 44
• Writing entire programs in pure style challenging
• Writing fragments of programs in pure style easy, useful
• + Easier to verify useful properties, debug
• + Better code generation
• − Challenge: interfacing pure and impure code
![Page 26: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/26.jpg)
Functional Factorial (yawn)
c© 2014– Andrei Alexandrescu. 18 / 44
ulong factorial(uint n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
• It’s PSPACE!
• Somebody should do hard time for this
![Page 27: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/27.jpg)
However, it’s pure
c© 2014– Andrei Alexandrescu. 19 / 44
pure ulong factorial(uint n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
• Pure is good
![Page 28: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/28.jpg)
Functional Factorial, Fixed
c© 2014– Andrei Alexandrescu. 20 / 44
pure ulong factorial(uint n) {
ulong crutch(uint n, ulong result) {
return n <= 1
? result
: crutch(n - 1, n * result);
}
return crutch(n, 1);
}
• Threads state through as parameters
• You know what? I don’t care for it
![Page 29: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/29.jpg)
Honest Factorial
c© 2014– Andrei Alexandrescu. 21 / 44
ulong factorial(uint n) {
ulong result = 1;
foreach (uint i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
• But no longer pure!
• Well allow me to retort
![Page 30: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/30.jpg)
![Page 31: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/31.jpg)
Pure is as pure does
c© 2014– Andrei Alexandrescu. 23 / 44
• “Pure functions always return the same result for the
same arguments”
• No reading and writing of global variables
◦ (Global constants okay)
• No calling of impure functions
• Who said anything about local, transient state inside the
function?
![Page 32: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/32.jpg)
Transitive State
c© 2014– Andrei Alexandrescu. 24 / 44
pure void reverse(T)(T[] a) {
foreach (i; 0 .. a.length / 2) {
swap(a[i], a[$ - i - 1]);
}
}
• Possibility: disallow
• More useful: relaxed rule
• Operate with transitive closure of state reachable
through parameter
• Not functional pure, but an interesting superset
• No need for another annotation, it’s all in the signature!
![Page 33: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/33.jpg)
User-defined types
c© 2014– Andrei Alexandrescu. 25 / 44
pure BigInt factorial(uint n) {
BigInt result = 1;
for (; n > 1; --n) {
result *= n;
}
return result;
}
• Better yet: purity deduced for generics and lambdas
![Page 34: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/34.jpg)
Aftermath
c© 2014– Andrei Alexandrescu. 26 / 44
• If parameters reach mutable state:
◦ Relaxed pure—no globals, no I/O, no impure calls
• If parameters can’t reach mutable state:
◦ “Haskell-grade” observed purity
◦ Yet imperative implementation possible
◦ As long as it’s local only
![Page 35: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/35.jpg)
Suddenly
c© 2014– Andrei Alexandrescu. 27 / 44
Combining purity with
mutability improves both
![Page 36: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/36.jpg)
The Generative Connection
c© 2014– Andrei Alexandrescu. 28 / 44
![Page 37: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/37.jpg)
Generative programming
c© 2014– Andrei Alexandrescu. 29 / 44
• In brief: code that generates code
• Generic programming often requires algorithm
specialization
• Specification often present in a DSL
![Page 38: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/38.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 30 / 44
Force into host language’s syntax?
![Page 39: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/39.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
![Page 40: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/40.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
• Regular expressions?
![Page 41: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/41.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
• Regular expressions?
• EBNF?
![Page 42: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/42.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
• Regular expressions?
• EBNF?
• PEG?
![Page 43: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/43.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
• Regular expressions?
• EBNF?
• PEG?
• SQL?
![Page 44: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/44.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 31 / 44
• Formatted printing?
• Regular expressions?
• EBNF?
• PEG?
• SQL?
• . . . Pasta for everyone!
![Page 45: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/45.jpg)
Embedded DSLs
c© 2014– Andrei Alexandrescu. 32 / 44
Here: use with native grammar
Process during compilation
Generate D code accordingly
![Page 46: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/46.jpg)
Compile-Time Evaluation
c© 2014– Andrei Alexandrescu. 33 / 44
• A large subset of D available for compile-time evaluation
ulong factorial(uint n) {
ulong result = 1;
for (; n > 1; --n) result *= n;
return result;
}
...
auto f1 = factorial(10); // run-time
static f2 = factorial(10); // compile-time
![Page 47: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/47.jpg)
Code injection with mixin
c© 2014– Andrei Alexandrescu. 34 / 44
mixin("writeln(\"hello, world\");");
mixin(generateSomeCode());
• Not as glamorous as AST manipulation but darn
effective
• Easy to understand and debug
• Now we have compile-time evaluation AND mixin. . .
![Page 48: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/48.jpg)
c© 2014– Andrei Alexandrescu. 35 / 44
Wait a minute!
![Page 49: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/49.jpg)
Example: bitfields in library
c© 2014– Andrei Alexandrescu. 36 / 44
struct A {
int a;
mixin(bitfields!(
uint, "x", 2,
int, "y", 3,
uint, "z", 2,
bool, "flag", 1));
}
A obj;
obj.x = 2;
obj.z = obj.x;
![Page 50: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/50.jpg)
Parser
c© 2014– Andrei Alexandrescu. 37 / 44
import pegged.grammar; // by Philippe Sigaud
mixin(grammar("
Expr < Factor AddExpr*
AddExpr < (’+’/’-’) Factor
Factor < Primary MulExpr*
MulExpr < (’*’/’/’) Primary
Primary < Parens / Number / Variable
/ ’-’ Primary
Parens < ’(’ Expr ’)’
Number <~ [0-9]+
Variable <- Identifier
"));
![Page 51: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/51.jpg)
Usage
c© 2014– Andrei Alexandrescu. 38 / 44
// Parsing at compile-time:
static parseTree1 = Expr.parse(
"1 + 2 - (3*x-5)*6");
pragma(msg, parseTree1.capture);
// Parsing at run-time:
auto parseTree2 = Expr.parse(readln());
writeln(parseTree2.capture);
![Page 52: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/52.jpg)
Scaling up
c© 2014– Andrei Alexandrescu. 39 / 44
1000 lines of D grammar →
3000 lines D parser
![Page 53: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/53.jpg)
c© 2014– Andrei Alexandrescu. 40 / 44
Highly integrated lex+yacc
![Page 54: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/54.jpg)
c© 2014– Andrei Alexandrescu. 41 / 44
What about regexen?
![Page 55: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/55.jpg)
Compile-time regular expressions
c© 2014– Andrei Alexandrescu. 42 / 44
• GSoC project by Dmitry Olshansky, merged into std
• Fully UTF capable, no special casing for ASCII
• Two modes sharing the same backend:
auto r1 = regex("^.*/([^/]+)/?$");
static r2 = ctRegex!("^.*/([^/]+)/?$");
• Run-time version uses intrinsics in a few places
• Static version generates specialized automaton, then
compiles it
![Page 56: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/56.jpg)
![Page 57: D Programming Language: The Sudden · Motivation c 2014– Andrei Alexandrescu. 7 / 44 • Systems-level programming a necessity • Several application categories • Faster is better—no](https://reader031.fdocuments.us/reader031/viewer/2022041808/5e564c648357a93fc100993f/html5/thumbnails/57.jpg)
Summary
c© 2014– Andrei Alexandrescu. 44 / 44
• Modularity + scoping
• Purity + sinning
• Compile-time evaluation + mixing code in