Skip to content

Commit a8a3132

Browse files
committed
Merge remote-tracking branch 'upstream/master' into stable
2 parents 9a91690 + b8dddee commit a8a3132

25 files changed

+1895
-238
lines changed

articles/cpptod.dd

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ $(H4 The D Way)
495495
Properties can be get and set using the normal field syntax,
496496
yet the get and set will invoke methods instead.
497497

498+
$(RUNNABLE_EXAMPLE
498499
------
499500
class Abc
500501
{
@@ -507,15 +508,13 @@ class Abc
507508
private:
508509
int myprop;
509510
}
510-
------
511-
512-
which is used as:
513511

514-
------
515512
Abc a = new Abc;
516513
a.property = 3;
517514
int x = a.property;
515+
assert(x == 3);
518516
------
517+
)
519518

520519
Thus, in D a property is treated like it was a simple field name.
521520
A property can start out actually being a simple field name,
@@ -563,6 +562,8 @@ $(H4 The D Way)
563562
$(HTTPS dlang.org/spec/template.html#implicit_template_properties,
564563
Eponymous Templates) - promotion of single template members to the
565564
enclosing name space:
565+
566+
$(RUNNABLE_EXAMPLE_COMPILE
566567
------
567568
template factorial(int n)
568569
{
@@ -574,19 +575,26 @@ template factorial(int n : 1)
574575
enum factorial = 1;
575576
}
576577

577-
void test()
578+
unittest
578579
{
580+
import std.stdio;
579581
writeln(factorial!(4)); // prints 24
580582
}
581583
------
584+
)
582585
The template blocks can be made shorter using
583586
$(HTTPS dlang.org/spec/template.html#variable-template,
584587
Enum Template) syntax:
588+
589+
$(RUNNABLE_EXAMPLE_COMPILE
585590
------
586591
enum factorial(int n) = n * .factorial!(n-1);
587592

588593
enum factorial(int n : 1) = 1;
594+
595+
static assert(factorial!(4) == 24);
589596
------
597+
)
590598

591599
<hr>$(COMMENT -------------------------------------------- )
592600

@@ -712,6 +720,7 @@ $(H4 The D Way)
712720
It compiles quickly, and gives a sensible compile time message
713721
if it fails.
714722

723+
$(RUNNABLE_EXAMPLE
715724
------
716725
import std.stdio;
717726

@@ -739,6 +748,7 @@ int main()
739748
return 0;
740749
}
741750
------
751+
)
742752

743753
<hr>$(COMMENT -------------------------------------------- )
744754

@@ -784,6 +794,7 @@ $(H4 The D Way)
784794
can be done in D without resorting to template argument
785795
pattern matching:
786796

797+
$(RUNNABLE_EXAMPLE_COMPILE
787798
------
788799
template IsFunctionT(T)
789800
{
@@ -793,27 +804,30 @@ template IsFunctionT(T)
793804
const int IsFunctionT = 1;
794805
}
795806

796-
void test()
807+
unittest
797808
{
798809
alias int fp(int);
799810

800-
assert(IsFunctionT!(fp) == 1);
811+
static assert(IsFunctionT!(fp) == 1);
801812
}
802813
------
814+
)
803815

804816
The task of discovering if a type is a function doesn't need a
805817
template at all, nor does it need the subterfuge of attempting to
806818
create the invalid array of functions type.
807819
The $(ISEXPRESSION) expression can test it directly:
808820

821+
$(RUNNABLE_EXAMPLE_COMPILE
809822
------
810-
void test()
823+
unittest
811824
{
812825
alias int fp(int);
813826

814-
assert( is(fp == function) );
827+
static assert( is(fp == function) );
815828
}
816829
------
830+
)
817831

818832
$(HR)
819833

@@ -928,7 +942,7 @@ void main()
928942

929943
auto d2 = gallery[0];
930944
d2.id = 1;
931-
assert(d2.id == gallery[0].id, "neither ref nor pointer");
945+
assert(d2.id != gallery[0].id); // d2 is neither ref nor pointer
932946
}
933947
------
934948
)
@@ -937,6 +951,8 @@ void main()
937951

938952
------
939953
auto d2 = &gallery[0];
954+
d2.id = 1;
955+
assert(d2.id == gallery[0].id);
940956
------
941957

942958
)

articles/ctod.dd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ array.length = array.length + 1;
325325
array[array.length - 1] = x;
326326
----------------------------
327327

328+
or simply:
329+
330+
----------------------------
331+
int[] array;
332+
int x;
333+
array ~= x;
334+
----------------------------
335+
336+
328337
<hr>$(COMMENT ============================================ )
329338
$(H2 $(LNAME2 strcat, String Concatenation))
330339

@@ -628,6 +637,27 @@ void dostring(string s)
628637
to generate a fast lookup scheme for it, eliminating the bugs
629638
and time required in hand-coding one.
630639

640+
Another possibility of associating a string to a number is to
641+
use an associative array.
642+
643+
$(RUNNABLE_EXAMPLE_COMPILE
644+
----------------------------
645+
ulong[string] map = ["hello" : 1616515,
646+
"goodbye" : 42,
647+
"maybe" : ulong.max
648+
];
649+
unittest
650+
{
651+
import std.stdio;
652+
writeln("number is ", map["goodbye"]);
653+
writeln(i"map contains $(map.length) elements");
654+
writeln("map is ", map);
655+
}
656+
----------------------------
657+
)
658+
659+
Any type can be used as key or as value.
660+
631661
<hr>$(COMMENT ============================================ )
632662
$(H2 $(LNAME2 align, Setting struct member alignment))
633663

book/d.en/is_expr.d

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ The following function template uses different specifiers with this syntax of th
344344
---
345345
void myFunction(T)(T parameter) {
346346
static if (is (T LocalAlias == struct)) {
347-
writefln("\n--- struct ---");
347+
writefln("\n-- struct --");
348348
// LocalAlias is the same as T. 'parameter' is the
349349
// struct object that has been passed to this
350350
// function.
@@ -355,7 +355,7 @@ void myFunction(T)(T parameter) {
355355
}
356356
357357
static if (is (T baseTypes == super)) {
358-
writeln("\n--- super ---");
358+
writeln("\n-- super --");
359359
// The 'baseTypes' tuple contains all of the base
360360
// types of T. 'parameter' is the class variable that
361361
// has been passed to this function.
@@ -368,7 +368,7 @@ void myFunction(T)(T parameter) {
368368
}
369369
370370
static if (is (T ImplT == enum)) {
371-
writeln("\n--- enum ---");
371+
writeln("\n-- enum --");
372372
// 'ImplT' is the actual implementation type of this
373373
// enum type. 'parameter' is the enum value that has
374374
// been passed to this function.
@@ -378,7 +378,7 @@ void myFunction(T)(T parameter) {
378378
}
379379
380380
static if (is (T ReturnT == return)) {
381-
writeln("\n--- return ---");
381+
writeln("\n-- return --");
382382
// 'ReturnT' is the return type of the function
383383
// pointer that has been passed to this function.
384384
@@ -418,18 +418,18 @@ The output:
418418
)
419419

420420
$(SHELL_SMALL
421-
--- struct ---
421+
-- struct --
422422
Constructing a new Point object by copying it.
423423

424-
--- super ---
424+
-- super --
425425
class AlarmClock has 2 base types.
426426
All of the bases: (Object, Clock)
427427
The topmost base: Object
428428

429-
--- enum ---
429+
-- enum --
430430
The implementation type of enum WeekDays is int
431431

432-
--- return ---
432+
-- return --
433433
This is a function with a return type of char:
434434
char function(double d, int i, Clock c)
435435
calling it... and the result is 'a'
@@ -497,7 +497,7 @@ The following program tests that $(C is) expression with four different types:
497497
import std.stdio;
498498

499499
void myFunction(T)(T parameter) {
500-
writefln("\n--- Called with %s ---", T.stringof);
500+
writefln("\n-- Called with %s --", T.stringof);
501501

502502
static if (is (T == Value[Key],
503503
Value,
@@ -533,20 +533,20 @@ The condition is satisfied only if the key type is $(C string):
533533
)
534534

535535
$(SHELL_SMALL
536-
--- Called with int ---
536+
-- Called with int --
537537
No, the condition has not been satisfied.
538538

539-
--- Called with int[string] ---
539+
-- Called with int[string] --
540540
Yes, the condition has been satisfied.
541541
The value type: int
542542
The key type : string
543543

544-
--- Called with double[string] ---
544+
-- Called with double[string] --
545545
Yes, the condition has been satisfied.
546546
The value type: double
547547
The key type : string
548548

549-
--- Called with dchar[long] ---
549+
-- Called with dchar[long] --
550550
No, the condition has not been satisfied.
551551
)
552552

book/d.en/literals.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ To see this logic in action, let's try the following program that takes advantag
7878
import std.stdio;
7979
8080
void main() {
81-
writeln("\n--- these are written in decimal ---");
81+
writeln("\n-- these are written in decimal --");
8282
8383
// fits an int, so the type is int
8484
writeln( 2_147_483_647, "\t\t",
@@ -88,7 +88,7 @@ void main() {
8888
writeln( 2_147_483_648, "\t\t",
8989
typeof(2_147_483_648).stringof);
9090
91-
writeln("\n--- these are NOT written in decimal ---");
91+
writeln("\n-- these are NOT written in decimal --");
9292
9393
// fits an int, so the type is int
9494
writeln( 0x7FFF_FFFF, "\t\t",
@@ -113,11 +113,11 @@ The output:
113113
)
114114
115115
$(SHELL
116-
--- these are written in decimal ---
116+
-- these are written in decimal --
117117
2147483647 int
118118
2147483648 long
119119
120-
--- these are NOT written in decimal ---
120+
-- these are NOT written in decimal --
121121
2147483647 int
122122
2147483648 uint
123123
4294967296 long

book/d.en/switch_case.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ import std.stdio;
123123
124124
void main() {
125125
foreach (value; [ 1, 2, 3, 10, 20 ]) {
126-
writefln("--- value: %s ---", value);
126+
writefln("-- value: %s --", value);
127127
128128
switch (value) {
129129
@@ -156,19 +156,19 @@ The output:
156156
)
157157
158158
$(SHELL
159-
--- value: 1 ---
159+
-- value: 1 --
160160
case 1
161161
case 2
162162
case 10
163-
--- value: 2 ---
163+
-- value: 2 --
164164
case 2
165165
case 10
166-
--- value: 3 ---
166+
-- value: 3 --
167167
case 3
168168
default
169-
--- value: 10 ---
169+
-- value: 10 --
170170
case 10
171-
--- value: 20 ---
171+
-- value: 20 --
172172
default
173173
)
174174

0 commit comments

Comments
 (0)