diff options
author | NickPapanastasiou <nickpap9411@gmail.com> | 2015-06-10 11:54:16 -0400 |
---|---|---|
committer | NickPapanastasiou <nickpap9411@gmail.com> | 2015-06-10 11:54:16 -0400 |
commit | 8f24f723e0b7cccde9dfcef2e2aa87d7eeb3822b (patch) | |
tree | b58217c473a6620b7b7c085fa8e18e46e171c9d7 | |
parent | f8da3b96ff93723f5e61aff24da7aa9e1c65f4a8 (diff) |
more D
-rw-r--r-- | .d.html.markdown.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | d.html.markdown | 32 |
2 files changed, 30 insertions, 2 deletions
diff --git a/.d.html.markdown.swp b/.d.html.markdown.swp Binary files differnew file mode 100644 index 00000000..5cd11360 --- /dev/null +++ b/.d.html.markdown.swp diff --git a/d.html.markdown b/d.html.markdown index f3836c2c..10a2be29 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -19,6 +19,8 @@ dudes. With all that out of the way, let's look at some examples! ```d // You know what's coming... +module hello; + import std.stdio; // args is optional @@ -65,7 +67,7 @@ we can use templates to parameterize all of these on both types and values! ```d // Here, T is a type parameter. Think <T> from C++/C#/Java -struct(T) LinkedList { +struct LinkedList(T) { T data = null; LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think <T> } @@ -90,7 +92,33 @@ enum Day { // 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; + + return a; +} + +// Use the ref keyword to pass by referece +void swap(T)(ref T a, ref T b) { + auto temp = a; + + a = b; + b = a; +} + +// With templates, we can also parameterize on values, not just types +class Matrix(T = int, uint m, uint n) { + T[m] rows; + T[n] columns; +} ``` + +Speaking of classes, let's talk about properties for a second. A property +is roughly a function that may act like an lvalue, so we can +have the syntax of POD structures (`structure.x = 7`) with the semantics of +getter and setter methods (`object.setX(7)`)! |