From e22187edc61489d2dd4df532a28a5096543533e2 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Thu, 12 Jan 2017 14:32:07 +0000 Subject: [PATCH 1/2] Tweak D for C, C++ programmers --- cpptod.dd | 25 +++++++++++++++++-------- ctod.dd | 26 +++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/cpptod.dd b/cpptod.dd index 83efe76167..0bc5b3e2ef 100644 --- a/cpptod.dd +++ b/cpptod.dd @@ -45,7 +45,7 @@ class Foo $(H4 The D Way) - Constructors are defined with the this keyword: + Constructors are defined with the `this` keyword: ------ class Foo @@ -76,7 +76,7 @@ class B : A $(H4 The D Way) - The base class constructor is called with the super syntax: + The base class constructor is called with the `super` syntax: ------ class A { this() { ... } } @@ -573,24 +573,33 @@ void test() $(H4 The D Way) The D version is analogous, though a little simpler, taking - advantage of promotion of single template members to the + advantage of + $(HTTPS dlang.org/spec/template.html#implicit_template_properties, + Eponymous Templates) - promotion of single template members to the enclosing name space: - ------ template factorial(int n) { - enum { factorial = n * .factorial!(n-1) } + enum factorial = n * .factorial!(n-1); } template factorial(int n : 1) { - enum { factorial = 1 } + enum factorial = 1; } void test() { - writefln("%d", factorial!(4)); // prints 24 + writeln(factorial!(4)); // prints 24 } +------ + The template blocks can be made shorter using + $(HTTPS dlang.org/spec/template.html#variable-template, + Enum Template) syntax: +------ +enum factorial(int n) = n * .factorial!(n-1); + +enum factorial(int n : 1) = 1; ------
$(COMMENT -------------------------------------------- ) @@ -800,7 +809,7 @@ template IsFunctionT(T) void test() { - typedef int fp(int); + alias int fp(int); assert(IsFunctionT!(fp) == 1); } diff --git a/ctod.dd b/ctod.dd index d40703191b..35f14036e9 100644 --- a/ctod.dd +++ b/ctod.dd @@ -78,7 +78,7 @@ sizeof(struct Foo) $(H4 The D Way) -$(P Use the size property:) +$(P Use the `sizeof` property:) ---------------------------- int.sizeof @@ -137,8 +137,8 @@ _Imaginary long double => ireal _Complex long double => creal ) $(P - Although char is an unsigned 8 bit type, and - wchar is an unsigned 16 bit type, they have their own separate types + Although `char` is an unsigned 8-bit type, and + `wchar` is an unsigned 16-bit type, they have their own separate types in order to aid overloading and type safety. ) $(P Ints and unsigneds in C are of varying size; not so in D.) @@ -194,7 +194,7 @@ long double r = fmodl(x,y); $(H4 The D Way) -D supports the remainder ('%') operator on floating point operands: +D supports the remainder (`%`) operator on floating point operands: ---------------------------- float f = x % y; @@ -295,7 +295,7 @@ for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) $(H4 The D Way) -The length of an array is accessible through the property "length". +The length of an array is accessible through the property `length`. ---------------------------- int[17] array; @@ -319,7 +319,7 @@ foreach (value; array) func(value); ---------------------------- -or through the ref keyword (for reference access): +or through the `ref` keyword (for reference access): ---------------------------- int[17] array; @@ -406,7 +406,7 @@ memcpy(s + lens, hello, sizeof(hello)); $(H4 The D Way) - D overloads the operators ~ and ~= for char and wchar arrays to mean + D overloads the operators `~` and `~=` for char and wchar arrays to mean concatenate and append, respectively: ---------------------------- @@ -582,7 +582,7 @@ $(H3 Struct tag name space) $(H4 The C Way) - It's annoying to have to put the struct keyword every time a type is specified, + It's annoying to have to put the `struct` keyword every time a type is specified, so a common idiom is to use: $(CCODE @@ -1181,7 +1181,7 @@ if (strcmp(str, "betty") == 0) // do strings match? $(H4 The D Way) - Why not use the == operator? + Why not use the `==` operator? ----------------------------- string str = "hello"; @@ -1394,7 +1394,7 @@ $(H3 Unsigned Right Shift) $(H4 The C Way) - The right shift operators >> and >>= are signed + The right shift operators `>>` and `>>=` are signed shifts if the left operand is a signed integral type, and are unsigned right shifts if the left operand is an unsigned integral type. To produce an unsigned right shift on an int, @@ -1422,9 +1422,9 @@ j = (unsigned)i >> 3; $(H4 The D Way) - D has the right shift operators >> and >>= which + D has the right shift operators `>>` and `>>=` which behave as they do in C. But D also has explicitly unsigned - right shift operators >>> and >>>= which will + right shift operators `>>>` and `>>>=` which will do an unsigned right shift regardless of the sign of the left operand. Hence, @@ -1579,7 +1579,7 @@ int main() $(H4 The D Way) - The ... following an array parameter declaration means that + The `...` following an array parameter declaration means that the trailing arguments are collected together to form an array. The arguments are type checked against the array type, and the number of arguments becomes a property From 551af09a6407be1fc6e42848c11eec49c09b2fad Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 13 Jan 2017 11:58:44 +0000 Subject: [PATCH 2/2] Make suggested edits --- ctod.dd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ctod.dd b/ctod.dd index 35f14036e9..6112b67316 100644 --- a/ctod.dd +++ b/ctod.dd @@ -295,7 +295,7 @@ for (i = 0; i < sizeof(array) / sizeof(array[0]); i++) $(H4 The D Way) -The length of an array is accessible through the property `length`. +The length of an array is accessible through the `length` property. ---------------------------- int[17] array; @@ -582,7 +582,7 @@ $(H3 Struct tag name space) $(H4 The C Way) - It's annoying to have to put the `struct` keyword every time a type is specified, + It's annoying to have to use the `struct` keyword every time a type is specified, so a common idiom is to use: $(CCODE