|
| 1 | +<h1>Dlang for JavaScript Developers - Functions and Control Flow</h1> |
| 2 | + |
| 3 | +<h2>Functions</h2> |
| 4 | +Dlang's function syntax has the return type in the place of the "function" keyword in JavaScript. Instead of typing the return type, you can the "auto" keyword for type inference. |
| 5 | + |
| 6 | +<pre> |
| 7 | +auto run() { |
| 8 | + auto income = 100; |
| 9 | + auto tax = calculate_tax(income); |
| 10 | + println!("{}", tax); |
| 11 | +} |
| 12 | + |
| 13 | +int calculate_tax(int income) { |
| 14 | + return income * 90 / 100; |
| 15 | +} |
| 16 | + |
| 17 | +</pre> |
| 18 | + |
| 19 | +The difference here is that the type comes before the name of the argument. |
| 20 | + |
| 21 | +<h2>Arrow Functions</h2> |
| 22 | +Arrow functions are a popular JavaScript feature. |
| 23 | + |
| 24 | +Dlang has lambdas and delegate literals, which are similar to arrow functions used in JavaScript. |
| 25 | + |
| 26 | +Without arguments: |
| 27 | +<pre> |
| 28 | +// JavaScript |
| 29 | +let greet = () => console.log("hello"); |
| 30 | + |
| 31 | +greet(); // "hello" |
| 32 | + |
| 33 | +// Dlang |
| 34 | +auto greet = () => writeln("hello"); |
| 35 | + |
| 36 | +greet(); |
| 37 | +</pre> |
| 38 | + |
| 39 | +With arguments: |
| 40 | +<pre> |
| 41 | +// JavaScript |
| 42 | +let greet = (msg) => console.log(msg); |
| 43 | + |
| 44 | +geet("good morning!"); // "good morning!" |
| 45 | + |
| 46 | +// Dlang |
| 47 | +auto greet = (string msg) => writeln(msg); |
| 48 | + |
| 49 | +greet("good morning!"); // "good morning!" |
| 50 | +</pre> |
| 51 | + |
| 52 | +Returning values: |
| 53 | +<pre> |
| 54 | +// JavaScript |
| 55 | +auto add = (a, b) => a + b; |
| 56 | + |
| 57 | +add(1, 2); // 3 |
| 58 | + |
| 59 | +// Dlang |
| 60 | +auto add = (int a, int b) => a + b; |
| 61 | + |
| 62 | +add(1, 2); // 3 |
| 63 | +</pre> |
| 64 | + |
| 65 | +This uses delegate syntax to accomplish multiline: |
| 66 | +<pre> |
| 67 | +// JavaScript |
| 68 | +let add = (a, b) => { |
| 69 | + let sum = a + b; |
| 70 | + return sum; |
| 71 | +}; |
| 72 | + |
| 73 | +add(1, 2); // 3 |
| 74 | + |
| 75 | +// Dlang |
| 76 | +auto add = (int a, int b) { |
| 77 | + auto sum = a + b; |
| 78 | + return sum; |
| 79 | +}; |
| 80 | +</pre> |
| 81 | + |
| 82 | +Closures don’t need the type annotations if the type annotation can be inferred by looking at the function call that receives the type annotation, but I’ve added them here for clarity. |
| 83 | + |
| 84 | +JavaScript has a stage 1 proposal for the pipeline operator. Dlang has a similar, more powerful syntax: the uniform function call syntax. (UFCS) |
| 85 | +<pre> |
| 86 | +// JavaScript (with transpiler) |
| 87 | + |
| 88 | +"hi" |> console.log; // "hi" |
| 89 | + |
| 90 | +// Dlang |
| 91 | + |
| 92 | +"hi".writeln; |
| 93 | +</pre> |
| 94 | + |
| 95 | +Furthermore, you can pass additional arguments after the pipeline operator: |
| 96 | +<pre> |
| 97 | +// JavaScript (with transpiler) |
| 98 | + |
| 99 | +"hi" |> (_ => console.log(_, "hello")); "hi hello" |
| 100 | + |
| 101 | +// Dlang |
| 102 | + |
| 103 | +"hi".writeln("hello"); |
| 104 | +</pre> |
| 105 | + |
| 106 | +<h2>If Else</h2> |
| 107 | +<pre> |
| 108 | +auto run() { |
| 109 | + auto income = 100; |
| 110 | + auto tax = calculate_tax(income); |
| 111 | + writeln(tax); |
| 112 | +} |
| 113 | +auto calculate_tax(int income) { |
| 114 | + if (income < 10) { |
| 115 | + return 0; |
| 116 | + } else if (income >= 10 && income < 50) { |
| 117 | + return 20; |
| 118 | + } else { |
| 119 | + return 50; |
| 120 | + } |
| 121 | +} |
| 122 | +</pre> |
| 123 | + |
| 124 | +<h2>Loops</h2> |
| 125 | + |
| 126 | +While loops: |
| 127 | +<pre> |
| 128 | +auto run() { |
| 129 | + auto count = 0; |
| 130 | + while (count < 10) { |
| 131 | + writeln(count); |
| 132 | + count += 1; |
| 133 | + } |
| 134 | +} |
| 135 | +</pre> |
| 136 | + |
| 137 | +Dlang offers foreach loops: |
| 138 | +<pre> |
| 139 | +auto run() { |
| 140 | + auto count = 0; |
| 141 | + auto numbers = [1, 2, 3, 4, 5]; |
| 142 | + foreach (n; numbers) { |
| 143 | + writeln(n); |
| 144 | + count += 1; |
| 145 | + } |
| 146 | +} |
| 147 | +</pre> |
| 148 | + |
| 149 | +As well as traditional for loops: |
| 150 | +<pre> |
| 151 | +auto run() { |
| 152 | + auto count = 0; |
| 153 | + auto numbers = [1, 2, 3, 4, 5]; |
| 154 | + for (auto i = 0; i < numbers.length; ++i) { |
| 155 | + writeln(numbers[i]); |
| 156 | + count += 1; |
| 157 | + } |
| 158 | +} |
| 159 | +</pre> |
| 160 | + |
| 161 | +We can also foreach-loop over ranges: |
| 162 | +<pre> |
| 163 | +auto run() { |
| 164 | + auto count = 0; |
| 165 | + foreach (n; 1..5) { |
| 166 | + writeln(n); |
| 167 | + count += 1; |
| 168 | + } |
| 169 | +} |
| 170 | +</pre> |
| 171 | + |
| 172 | +<h2>Ranges</h2> |
| 173 | + |
| 174 | +<i>TODO</i> |
| 175 | + |
| 176 | +<hr/> |
| 177 | + |
| 178 | +I saw <a href="http://www.sheshbabu.com/posts/rust-for-javascript-developers-functions-and-control-flow/">Rust for JavaScript Developers - Functions and Control Flow</a> and just had to write this post for Dlang. |
| 179 | + |
| 180 | + |
| 181 | +Shoot me an email if you want me to finish this or write the next part on how Dlang's const and immutable work and emulating proxies in Dlang. |
| 182 | + |
0 commit comments