add section about string mixins

This commit is contained in:
Johannes Loher 2018-05-30 16:26:53 +02:00
parent 9b06a68383
commit 74257052c0

View file

@ -84,6 +84,7 @@
- [Example](#example) - [Example](#example)
- [Executing unittests](#executing-unittests) - [Executing unittests](#executing-unittests)
- [Code coverage](#code-coverage) - [Code coverage](#code-coverage)
- [String mixins](#string-mixins)
## Setup ## Setup
@ -1240,7 +1241,6 @@ composed to express complex operations in a readable way. They are based on
ranges and will work on your datatypes, if they implement the range interface. ranges and will work on your datatypes, if they implement the range interface.
```D ```D
// Hey come on, just get the whole army!
import std.algorithm : canFind, map, import std.algorithm : canFind, map,
filter, sort, uniq, joiner, chunkBy, splitter; filter, sort, uniq, joiner, chunkBy, splitter;
import std.array : array, empty; import std.array : array, empty;
@ -1248,12 +1248,11 @@ import std.range : zip;
import std.stdio : writeln; import std.stdio : writeln;
import std.string : format; import std.string : format;
void main() void main() {
{ string text = `This tour will give you an
string text = q{This tour will give you an
overview of this powerful and expressive systems overview of this powerful and expressive systems
programming language which compiles directly programming language which compiles directly
to efficient, *native* machine code.}; to efficient, *native* machine code.`;
// splitting predicate // splitting predicate
alias pred = c => canFind(" ,.\n", c); alias pred = c => canFind(" ,.\n", c);
@ -1297,8 +1296,7 @@ D has built-in unittests via a very simple syntax, which can appear anywhere in
a D module: a D module:
```D ```D
unittest unittest {
{
assert(myAbs(-1) == 1); assert(myAbs(-1) == 1);
assert(myAbs(1) == 1); assert(myAbs(1) == 1);
} }
@ -1334,8 +1332,7 @@ struct Vector3 {
} }
} }
void main() void main() {
{
Vector3 vec = Vector3(0,1,0); Vector3 vec = Vector3(0,1,0);
writeln(`This vector has been tested: `, vec); writeln(`This vector has been tested: `, vec);
} }
@ -1343,8 +1340,6 @@ void main()
// Or just somewhere else // Or just somewhere else
unittest { unittest {
Vector3 vec; Vector3 vec;
// .init a special built-in property that
// returns the initial value of type.
assert(vec.x == double.init); assert(vec.x == double.init);
} }
``` ```
@ -1359,3 +1354,20 @@ using DUB, just run `dub test`.
D also supports generating coverage reports out of the box, just compile with D also supports generating coverage reports out of the box, just compile with
the `-cov` flag. When using DUB, just add the flag `--coverage`. the `-cov` flag. When using DUB, just add the flag `--coverage`.
### String mixins
The mixin expression takes an arbitrary string and compiles it and generates instructions accordingly. This is a compile-time feature, so ths string needs
to be known at compile-time.
```D
mixin("int b = 5"); // compiles just fine
assert(b == 5);
static immutable compileTimeKnownString = q{int c = 5;};
mixin(compileTimeKnownString); // so does this
assert(c == 5);
immutable runTimeString = q{int c =5;};
mixin(runTimeString); // compile error, runTimeString is not known at compile-time
```