|
@@ -107,10 +107,15 @@ my_func(X) -> X * 4.
|
|
|
->
|
|
|
```dlang
|
|
|
int my_func(int x) {
|
|
|
- if (x == 1) return 1;
|
|
|
- else if (x == 5) return x * 2;
|
|
|
- else if (x == 9) return x * 3;
|
|
|
- else return x * 4; // other cases
|
|
|
+ if (x == 1){
|
|
|
+ return 1;
|
|
|
+ }else if (x == 5){
|
|
|
+ return x * 2;
|
|
|
+ }else if (x == 9){
|
|
|
+ return x * 3;
|
|
|
+ }else{ // other cases
|
|
|
+ return x * 4;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// or
|
|
@@ -130,8 +135,102 @@ int my_func(int x) {
|
|
|
```
|
|
|
note that compiler can optimise few comparison conditions for ```switch``` better than that in ```if .. else```.
|
|
|
|
|
|
-todo add examples
|
|
|
|
|
|
+### 2.4.1 pattern matching examples
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+int = int x
|
|
|
+my_func 1 = 1
|
|
|
+my_func 5 = 5 * 2
|
|
|
+my_func 9 = 9 * 3
|
|
|
+my_func X = X * 4
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+int my_func(int x) {
|
|
|
+ if (x == 1){
|
|
|
+ return 1;
|
|
|
+ }else if (x == 5){
|
|
|
+ return 5 * 2; // return x * 2;
|
|
|
+ }else if (x == 9){
|
|
|
+ return 9 * 3; // return x * 3;
|
|
|
+ }else{ // other cases
|
|
|
+ return x * 4;
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// left = function type, right = arguments type
|
|
|
+// sw = use switch
|
|
|
+int sw = int x
|
|
|
+my_func 1 = 1
|
|
|
+my_func 5 = 5 * 2
|
|
|
+my_func 9 = 9 * 3
|
|
|
+my_func X = X * 4
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+int my_func(int x) {
|
|
|
+ switch (x) {
|
|
|
+ case 1:
|
|
|
+ return 1; // if x == 1
|
|
|
+ case 5:
|
|
|
+ return 5 * 2; // return x * 2; // if x == 5
|
|
|
+ case 9:
|
|
|
+ return 9 * 3; // return x * 3; // if x == 9
|
|
|
+ default:
|
|
|
+ return x * 4; // other cases
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+y = case x
|
|
|
+ 1 = 1
|
|
|
+ 5 = 5 * 2
|
|
|
+ 9 = 9 * 3
|
|
|
+ _ = x * 4
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+if (x == 1){
|
|
|
+ y = 1;
|
|
|
+}else if (x == 5){
|
|
|
+ y = 5 * 2; // return x * 2;
|
|
|
+}else if (x == 9){
|
|
|
+ y = 9 * 3; // return x * 3;
|
|
|
+}else{ // other cases
|
|
|
+ y = x * 4;
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// sw case = use switch in dlang for this case
|
|
|
+y = sw case x
|
|
|
+ 1 = 1
|
|
|
+ 5 = 5 * 2
|
|
|
+ 9 = 9 * 3
|
|
|
+ _ = x * 4
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+switch (x) {
|
|
|
+ case 1:
|
|
|
+ y = 1; // if x == 1
|
|
|
+ case 5:
|
|
|
+ y = 5 * 2; // y = x * 2; // if x == 5
|
|
|
+ case 9:
|
|
|
+ y = 9 * 3; // y = x * 3; // if x == 9
|
|
|
+ default:
|
|
|
+ y = x * 4; // other cases
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+todo more examples -- with switch case dlang
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
|
|
|
## 2.5 aliases for imports, externs etc
|
|
|
```
|
|
@@ -146,10 +245,72 @@ import std.array;
|
|
|
import std.string;
|
|
|
import std.algorithm;
|
|
|
```
|
|
|
+
|
|
|
+```
|
|
|
+use core.stdc.stdio [ printf scanf ]
|
|
|
+ std.conv [ to ]
|
|
|
+
|
|
|
+->
|
|
|
+import core.stdc.stdio : printf, scanf;
|
|
|
+import std.conv : to;
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+use as use_std1
|
|
|
+ core.stdc.stdio [ printf scanf ]
|
|
|
+ std.conv [ to ]
|
|
|
+
|
|
|
+
|
|
|
+use_std1 // use elsewhere in the file
|
|
|
+
|
|
|
+->
|
|
|
+import core.stdc.stdio : printf, scanf;
|
|
|
+import std.conv : to;
|
|
|
+```
|
|
|
todo add more examples
|
|
|
|
|
|
|
|
|
-## 2.6 code between ```dlang``` reserved words translates into target .d file without changes
|
|
|
+## 2.6 main, wasm import from js (wasm export to js = as usual function + return)
|
|
|
+```
|
|
|
+main
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+void main()
|
|
|
+{
|
|
|
+...
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+smain
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+extern(C) void main()
|
|
|
+{
|
|
|
+...
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// for wasm
|
|
|
+wmain
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+void _start(){ }
|
|
|
+```
|
|
|
+
|
|
|
+```
|
|
|
+// wasm import from js
|
|
|
+wuse print int a
|
|
|
+
|
|
|
+->
|
|
|
+
|
|
|
+void print(int a);
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+## 2.7 code between ```dlang``` reserved words translates into target .d file without changes
|
|
|
```
|
|
|
...
|
|
|
|
|
@@ -163,7 +324,7 @@ dlang
|
|
|
```
|
|
|
|
|
|
|
|
|
-## 2.7 reserved words (keywords) list
|
|
|
+## 2.8 reserved words (keywords) list
|
|
|
including https://dlang.org/spec/lex.html#keywords
|
|
|
```
|
|
|
abstract alias align asm assert auto
|