add section about string mixins
This commit is contained in:
parent
9b06a68383
commit
74257052c0
1 changed files with 23 additions and 11 deletions
|
@ -84,6 +84,7 @@
|
|||
- [Example](#example)
|
||||
- [Executing unittests](#executing-unittests)
|
||||
- [Code coverage](#code-coverage)
|
||||
- [String mixins](#string-mixins)
|
||||
|
||||
## 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.
|
||||
|
||||
```D
|
||||
// Hey come on, just get the whole army!
|
||||
import std.algorithm : canFind, map,
|
||||
filter, sort, uniq, joiner, chunkBy, splitter;
|
||||
import std.array : array, empty;
|
||||
|
@ -1248,12 +1248,11 @@ import std.range : zip;
|
|||
import std.stdio : writeln;
|
||||
import std.string : format;
|
||||
|
||||
void main()
|
||||
{
|
||||
string text = q{This tour will give you an
|
||||
void main() {
|
||||
string text = `This tour will give you an
|
||||
overview of this powerful and expressive systems
|
||||
programming language which compiles directly
|
||||
to efficient, *native* machine code.};
|
||||
to efficient, *native* machine code.`;
|
||||
|
||||
// splitting predicate
|
||||
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:
|
||||
|
||||
```D
|
||||
unittest
|
||||
{
|
||||
unittest {
|
||||
assert(myAbs(-1) == 1);
|
||||
assert(myAbs(1) == 1);
|
||||
}
|
||||
|
@ -1334,8 +1332,7 @@ struct Vector3 {
|
|||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
void main() {
|
||||
Vector3 vec = Vector3(0,1,0);
|
||||
writeln(`This vector has been tested: `, vec);
|
||||
}
|
||||
|
@ -1343,8 +1340,6 @@ void main()
|
|||
// Or just somewhere else
|
||||
unittest {
|
||||
Vector3 vec;
|
||||
// .init a special built-in property that
|
||||
// returns the initial value of type.
|
||||
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
|
||||
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
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue