|
@@ -81,11 +81,11 @@ todo add examples
|
|
|
## 2.4 Pattern Matching at functions too (like in Erlang)
|
|
|
Function overloading in dlang allows to define the same function but with different versions for different data types:
|
|
|
```dlang
|
|
|
-int my_func(int x) {
|
|
|
+int my_func(int x){
|
|
|
return x;
|
|
|
}
|
|
|
|
|
|
-int my_func(string x) {
|
|
|
+int my_func(string x){
|
|
|
return x.length; // or other operation(s) with argument
|
|
|
}
|
|
|
```
|
|
@@ -104,12 +104,12 @@ my_func(X) -> X * 4.
|
|
|
```
|
|
|
->
|
|
|
```dlang
|
|
|
-int my_func(int x) {
|
|
|
- if (x == 1){
|
|
|
+int my_func(int x){
|
|
|
+ if(x == 1){
|
|
|
return 1;
|
|
|
- }else if (x == 5){
|
|
|
+ }else if(x == 5){
|
|
|
return x * 2;
|
|
|
- }else if (x == 9){
|
|
|
+ }else if(x == 9){
|
|
|
return x * 3;
|
|
|
}else{ // other cases
|
|
|
return x * 4;
|
|
@@ -118,8 +118,8 @@ int my_func(int x) {
|
|
|
|
|
|
// or
|
|
|
|
|
|
-int my_func(int x) {
|
|
|
- switch (x) {
|
|
|
+int my_func(int x){
|
|
|
+ switch (x){
|
|
|
case 1:
|
|
|
return 1; // if x == 1
|
|
|
case 5:
|
|
@@ -226,13 +226,13 @@ bool has_begin_test(string str, out string rest){
|
|
|
void main(){
|
|
|
string rest;
|
|
|
|
|
|
- writeln(has_begin_test("test123", rest)); // true, rest = "123"
|
|
|
+ writeln( has_begin_test("test123", rest) ); // true, rest = "123"
|
|
|
writeln(rest); // "123"
|
|
|
|
|
|
- writeln(has_begin_test("test", rest)); // true, rest = ""
|
|
|
+ writeln( has_begin_test("test", rest) ); // true, rest = ""
|
|
|
writeln(rest); // ""
|
|
|
|
|
|
- writeln(has_begin_test("example", rest)); // false, rest = "example"
|
|
|
+ writeln( has_begin_test("example", rest) ); // false, rest = "example"
|
|
|
writeln(rest); // "example"
|
|
|
}
|
|
|
*/
|
|
@@ -277,16 +277,16 @@ bool has_end_test(string str, out string rest){
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
-void main() {
|
|
|
+void main(){
|
|
|
string rest;
|
|
|
|
|
|
- writeln(has_end_test("hellotest", rest)); // true, rest = "hello"
|
|
|
+ writeln( has_end_test("hellotest", rest) ); // true, rest = "hello"
|
|
|
writeln(rest); // "hello"
|
|
|
|
|
|
- writeln(has_end_test("example", rest)); // false, rest = "example"
|
|
|
+ writeln( has_end_test("example", rest) ); // false, rest = "example"
|
|
|
writeln(rest); // "example"
|
|
|
|
|
|
- writeln(has_end_test("test", rest)); // true, rest = ""
|
|
|
+ writeln( has_end_test("test", rest) ); // true, rest = ""
|
|
|
writeln(rest); // ""
|
|
|
}
|
|
|
*/
|
|
@@ -302,12 +302,12 @@ my_func X = X * 4
|
|
|
|
|
|
->
|
|
|
|
|
|
-int my_func(int x) {
|
|
|
- if (x == 1){
|
|
|
+int my_func(int x){
|
|
|
+ if(x == 1){
|
|
|
return 1;
|
|
|
- }else if (x == 5){
|
|
|
+ }else if(x == 5){
|
|
|
return 5 * 2; // return x * 2;
|
|
|
- }else if (x == 9){
|
|
|
+ }else if(x == 9){
|
|
|
return 9 * 3; // return x * 3;
|
|
|
}else{ // other cases
|
|
|
return x * 4;
|
|
@@ -326,8 +326,8 @@ my_func_sw X = X * 4
|
|
|
|
|
|
->
|
|
|
|
|
|
-int my_func(int x) {
|
|
|
- switch (x) {
|
|
|
+int my_func(int x){
|
|
|
+ switch(x){
|
|
|
case 1:
|
|
|
return 1; // if x == 1
|
|
|
case 5:
|
|
@@ -352,9 +352,9 @@ y = ptn x
|
|
|
|
|
|
if (x == 1){
|
|
|
y = 1;
|
|
|
-}else if (x == 5){
|
|
|
+}else if(x == 5){
|
|
|
y = 5 * 2; // return x * 2;
|
|
|
-}else if (x == 9){
|
|
|
+}else if(x == 9){
|
|
|
y = 9 * 3; // return x * 3;
|
|
|
}else{ // other cases
|
|
|
y = x * 4;
|
|
@@ -371,7 +371,7 @@ y = ptns x
|
|
|
|
|
|
->
|
|
|
|
|
|
-switch (x) {
|
|
|
+switch(x){
|
|
|
case 1:
|
|
|
y = 1; // if x == 1
|
|
|
case 5:
|
|
@@ -567,6 +567,9 @@ as
|
|
|
ptn
|
|
|
ptns
|
|
|
|
|
|
+_
|
|
|
+++
|
|
|
+
|
|
|
*_sw (function name end)
|
|
|
|
|
|
main
|