From c5ac70f706802e9642c7ddc81caf88482a0eb2c6 Mon Sep 17 00:00:00 2001 From: NickPapanastasiou Date: Sun, 7 Jun 2015 22:50:05 -0400 Subject: D stuff --- d.html.markdown | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'd.html.markdown') diff --git a/d.html.markdown b/d.html.markdown index a6245a99..be9f8fb0 100644 --- a/d.html.markdown +++ b/d.html.markdown @@ -50,12 +50,47 @@ void main() { writeln(i); } - foreach_reverse(i; short.max) { + foreach_reverse(i; 1..short.max) { if(n % 2 == 1) writeln(i); else writeln("No!"); } } +``` + +We can define new types and functions with `struct`, `class`, `union`, and `enum`. Structs and unions +are passed to functions by value (i.e. copied) and classes are passed by reference. Futhermore, +we can use templates to parameterize all of these on both types and values! + +``` +// Here, T is a type parameter. Think from C++/C#/Java +struct(T) LinkedList { + T data = null; + LinkedList!(T)* next; // The ! is used to instaniate a parameterized type. Again, think +} + +class BinTree(T) { + T data = null; + + BinTree!T left; + BinTree!T right; +} + +enum Day { + Sunday, + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, +} + +// Use alias to create abbreviations for types + +alias IntList = LinkedList!int; + +alias NumTree = BinTree!double; ``` -- cgit v1.2.3