From a4d9115decdfaa0346c66994df372e529d343d37 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:15:49 +0200 Subject: Clarify that not just two people develop D The present construction seems to imply that only Walter and Andrei and involved in the development of D. This is nowhere close to being true! --- d.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index ba24b60f..7c23f2dd 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -23,8 +23,8 @@ about [D](http://dlang.org/). The D programming language is a modern, general-pu multi-paradigm language with support for everything from low-level features to expressive high-level abstractions. -D is actively developed by Walter Bright and Andrei Alexandrescu, two super smart, really cool -dudes. With all that out of the way, let's look at some examples! +D is actively developed by a large group of super-smart people and is spearheaded by Walter Bright +and Andrei Alexandrescu. With all that out of the way, let's look at some examples! ```c import std.stdio; -- cgit v1.2.3 From 064b82eab443fa1bc8c1dd0b061bedbc04b60e66 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:16:30 +0200 Subject: Add wikipedia page links Link to the wikipedia pages of Walter Bright and Andrei Alexandrescu in an attempt to at least partially establish their credibility. --- d.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 7c23f2dd..d56e08a6 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -23,8 +23,10 @@ about [D](http://dlang.org/). The D programming language is a modern, general-pu multi-paradigm language with support for everything from low-level features to expressive high-level abstractions. -D is actively developed by a large group of super-smart people and is spearheaded by Walter Bright -and Andrei Alexandrescu. With all that out of the way, let's look at some examples! +D is actively developed by a large group of super-smart people and is spearheaded by +[Walter Bright](https://en.wikipedia.org/wiki/Walter_Bright) and +[Andrei Alexandrescu](https://en.wikipedia.org/wiki/Andrei_Alexandrescu). +With all that out of the way, let's look at some examples! ```c import std.stdio; -- cgit v1.2.3 From 4be1044a64e7ac1000a458087ee9131a9999d05f Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Tue, 13 Oct 2015 18:17:11 +0200 Subject: Improve code comments --- d.html.markdown | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index d56e08a6..88a83e41 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -38,9 +38,10 @@ void main() { writeln(i); } - auto n = 1; // use auto for type inferred variables + // 'auto' can be used for inferring types. + auto n = 1; - // Numeric literals can use _ as a digit seperator for clarity + // Numeric literals can use '_' as a digit separator for clarity. while(n < 10_000) { n += n; } @@ -49,13 +50,15 @@ void main() { n -= (n / 2); } while(n > 0); - // For and while are nice, but in D-land we prefer foreach - // The .. creates a continuous range, excluding the end + // For and while are nice, but in D-land we prefer 'foreach' loops. + // The '..' creates a continuous range, including the first value + // but excluding the last. foreach(i; 1..1_000_000) { if(n % 2 == 0) writeln(i); } + // There's also 'foreach_reverse' when you want to loop backwards. foreach_reverse(i; 1..int.max) { if(n % 2 == 1) { writeln(i); @@ -80,7 +83,7 @@ struct LinkedList(T) { class BinTree(T) { T data = null; - // If there is only one template parameter, we can omit parens + // If there is only one template parameter, we can omit the parentheses BinTree!T left; BinTree!T right; } -- cgit v1.2.3 From d136abe95434a94bd70a981b1ea2bf0ee1dbf636 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Thu, 15 Oct 2015 07:31:48 +0200 Subject: Improve code comments --- d.html.markdown | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index 88a83e41..ea1c1700 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -74,16 +74,18 @@ are passed to functions by value (i.e. copied) and classes are passed by referen we can use templates to parameterize all of these on both types and values! ```c -// Here, T is a type parameter. Think from C++/C#/Java +// Here, 'T' is a type parameter. Think '' from C++/C#/Java. struct LinkedList(T) { T data = null; - LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think + + // Use '!' to instantiate a parameterized type. Again, think ''. + LinkedList!(T)* next; } class BinTree(T) { T data = null; - // If there is only one template parameter, we can omit the parentheses + // If there is only one template parameter, we can omit the parentheses. BinTree!T left; BinTree!T right; } @@ -98,13 +100,11 @@ enum Day { Saturday, } -// Use alias to create abbreviations for types - +// Use alias to create abbreviations for types. alias IntList = LinkedList!int; alias NumTree = BinTree!double; // We can create function templates as well! - T max(T)(T a, T b) { if(a < b) return b; @@ -112,9 +112,8 @@ T max(T)(T a, T b) { return a; } -// Use the ref keyword to ensure pass by referece. -// That is, even if a and b are value types, they -// will always be passed by reference to swap +// Use the ref keyword to ensure pass by reference. That is, even if 'a' and 'b' +// are value types, they will always be passed by reference to 'swap()'. void swap(T)(ref T a, ref T b) { auto temp = a; @@ -122,13 +121,13 @@ void swap(T)(ref T a, ref T b) { b = temp; } -// With templates, we can also parameterize on values, not just types +// With templates, we can also parameterize on values, not just types. class Matrix(uint m, uint n, T = int) { T[m] rows; T[n] columns; } -auto mat = new Matrix!(3, 3); // We've defaulted type T to int +auto mat = new Matrix!(3, 3); // We've defaulted type 'T' to 'int'. ``` @@ -138,21 +137,20 @@ have the syntax of POD structures (`structure.x = 7`) with the semantics of getter and setter methods (`object.setX(7)`)! ```c -// Consider a class parameterized on a types T, U - +// Consider a class parameterized on types 'T' & 'U'. class MyClass(T, U) { T _data; U _other; - } -// And "getter" and "setter" methods like so +// And "getter" and "setter" methods like so: class MyClass(T, U) { T _data; U _other; - // Constructors are always named `this` + // Constructors are always named 'this'. this(T t, U u) { + // This will call the setter methods below. data = t; other = u; } @@ -175,8 +173,8 @@ class MyClass(T, U) { _other = u; } } -// And we use them in this manner +// And we use them in this manner: void main() { auto mc = MyClass!(int, string); -- cgit v1.2.3 From a77d0264c9df6ac2ac4c1e2202e352704580c579 Mon Sep 17 00:00:00 2001 From: Gautam Kotian Date: Thu, 15 Oct 2015 07:32:48 +0200 Subject: Make the code compilable and add some additional comments to explain what's happening. --- d.html.markdown | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index ea1c1700..80c1dc65 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -176,13 +176,21 @@ class MyClass(T, U) { // And we use them in this manner: void main() { - auto mc = MyClass!(int, string); + auto mc = new MyClass!(int, string)(7, "seven"); - mc.data = 7; - mc.other = "seven"; + // Import the 'stdio' module from the standard library for writing to + // console (imports can be local to a scope). + import std.stdio; - writeln(mc.data); - writeln(mc.other); + // Call the getters to fetch the values. + writefln("Earlier: data = %d, str = %s", mc.data, mc.other); + + // Call the setters to assign new values. + mc.data = 8; + mc.other = "eight"; + + // Call the getters again to fetch the new values. + writefln("Later: data = %d, str = %s", mc.data, mc.other); } ``` -- cgit v1.2.3