Improve code comments

This commit is contained in:
Gautam Kotian 2015-10-15 07:31:48 +02:00
parent 4be1044a64
commit d136abe954

View File

@ -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! we can use templates to parameterize all of these on both types and values!
```c ```c
// Here, T is a type parameter. Think <T> from C++/C#/Java // Here, 'T' is a type parameter. Think '<T>' from C++/C#/Java.
struct LinkedList(T) { struct LinkedList(T) {
T data = null; T data = null;
LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think <T>
// Use '!' to instantiate a parameterized type. Again, think '<T>'.
LinkedList!(T)* next;
} }
class BinTree(T) { class BinTree(T) {
T data = null; 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 left;
BinTree!T right; BinTree!T right;
} }
@ -98,13 +100,11 @@ enum Day {
Saturday, Saturday,
} }
// Use alias to create abbreviations for types // Use alias to create abbreviations for types.
alias IntList = LinkedList!int; alias IntList = LinkedList!int;
alias NumTree = BinTree!double; alias NumTree = BinTree!double;
// We can create function templates as well! // We can create function templates as well!
T max(T)(T a, T b) { T max(T)(T a, T b) {
if(a < b) if(a < b)
return b; return b;
@ -112,9 +112,8 @@ T max(T)(T a, T b) {
return a; return a;
} }
// Use the ref keyword to ensure pass by referece. // Use the ref keyword to ensure pass by reference. That is, even if 'a' and 'b'
// That is, even if a and b are value types, they // are value types, they will always be passed by reference to 'swap()'.
// will always be passed by reference to swap
void swap(T)(ref T a, ref T b) { void swap(T)(ref T a, ref T b) {
auto temp = a; auto temp = a;
@ -122,13 +121,13 @@ void swap(T)(ref T a, ref T b) {
b = temp; 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) { class Matrix(uint m, uint n, T = int) {
T[m] rows; T[m] rows;
T[n] columns; 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)`)! getter and setter methods (`object.setX(7)`)!
```c ```c
// Consider a class parameterized on a types T, U // Consider a class parameterized on types 'T' & 'U'.
class MyClass(T, U) { class MyClass(T, U) {
T _data; T _data;
U _other; U _other;
} }
// And "getter" and "setter" methods like so // And "getter" and "setter" methods like so:
class MyClass(T, U) { class MyClass(T, U) {
T _data; T _data;
U _other; U _other;
// Constructors are always named `this` // Constructors are always named 'this'.
this(T t, U u) { this(T t, U u) {
// This will call the setter methods below.
data = t; data = t;
other = u; other = u;
} }
@ -175,8 +173,8 @@ class MyClass(T, U) {
_other = u; _other = u;
} }
} }
// And we use them in this manner
// And we use them in this manner:
void main() { void main() {
auto mc = MyClass!(int, string); auto mc = MyClass!(int, string);