ballerina 学习十五 控制流
ballerina 的控制流没有什么特殊,只是相比一般语言多了一个模式匹配的操作match ,实际上其他语言(erlang elixir rust 中的模式匹配是很强大的)
简单例子
- if/else
import ballerina/io;
function main(string… args) {
int a = 10;
int b = 0;
if (a == 10) {
io:println("a == 10");
}
if (a < b) {
io:println("a < b");
} else {
io:println("a >= b");
}
if (b < 0) {
io:println("b < 0");
} else if (b > 0) {
io:println("b > 0");
} else {
io:println("b == 0");
}
}
- while
import ballerina/io;
function main(string… args) {
int i = 0;
while (i < 3) {
io:println(i);
i = i + 1;
}
int j = 0;
while (j < 5) {
io:println(j);
j = j + 1;
if (j == 3) {
break;
}
}
int k = 0;
while (k < 5) {
if (k < 3) {
k = k + 1;
next;
}
io:println(k);
k = k + 1;
}
}
- match
import ballerina/io;
type KeyNotFoundError {
string message;
error? cause;
string key;
};
map<string?> values = {"key1": "value1", "key2": ()};
function getValue(string key) returns string?|KeyNotFoundError {
if (!values.hasKey(key)) {
KeyNotFoundError err = {message: "key '" + key + "' not found", key: key};
return err;
} else {
return values[key];
}
}
function print(string?|KeyNotFoundError result) {
match result {
string value => io:println("value: " + value);
() => io:println("value is ()");
KeyNotFoundError e => {
io:println(e.message);
}
}
}
function main(string… args) {
print(getValue("key1"));
print(getValue("key2"));
print(getValue("key3"));
}
- foreach
import ballerina/io;
function main(string… args) {
io:println("Iterating over a string array:-");
string[] fruits = ["apple", "banana", "cherry"];
foreach v in fruits {
io:println("fruit: " + v);
}
io:println("\nIterating over a map:-");
map<string> words = { a: "apple", b: "banana", c: "cherry" };
foreach k, v in words {
io:println("letter: ", k, ", word: ", v);
}
io:println("\nIterating over a json object:-");
json apple = { name: "apple", colors: ["red", "green"], price: 5 };
foreach j in apple {
match j {
string js => {
io:println("string value: ", js);
}
json jx => {
io:println("non-string value: ", jx);
}
}
}
io:println("\nIterating over a json array:-");
json[] colors = check <json[]>apple.colors;
foreach i, j in colors {
io:println("color ", i, ": ", j);
}
io:println("\nIterating over an xml:-");
xml book = xml `<book>
<name>Sherlock Holmes</name>
<author>Sir Arthur Conan Doyle</author>
</book>`;
foreach i, x in book.*.elements(){
io:println("xml at ", i, ": ", x);
}
io:println("\nIterating over an integer range:-");
int endValue = 10;
int sum;
foreach i in [1..endValue] {
sum = sum + i;
}
io:println("summation from 1 to " + endValue + " is " + sum);
}
- match expression ( 类似rust result)
import ballerina/io;
function getAgeCategory(int age) returns string|error {
if (age < 0) {
error e = { message: "Invalid" };
return e;
} else if (age <= 18) {
return "Child";
} else {
return "Adult";
}
}
function main(string… args) {
string ageCategory = getAgeCategory(25) but {
string s => s,
error e => e.message
};
io:println(ageCategory);
ageCategory = getAgeCategory(-5) but {
string s => s,
error e => e.message
};
io:println(ageCategory);
ageCategory = getAgeCategory(25) but {
error e => e.message
};
io:println(ageCategory);
}
- elvis (
import ballerina/io;
function main(string… args) {
string|() x = null;
string matchExprOutput;
matchExprOutput = x but {
string s => s,
() => "value is null"
};
io:println("The output from match expression: ", matchExprOutput);
string elvisOutput = x ?: "value is null";
io:println("The output from elvis operator: ", elvisOutput);
}
参考资料
https://ballerina.io/learn/by-example/elvis-operator.html
https://ballerina.io/learn/by-example/match-expression.html
https://ballerina.io/learn/by-example/foreach.html
https://ballerina.io/learn/by-example/match.html
https://ballerina.io/learn/by-example/if-else.html
https://ballerina.io/learn/by-example/while.html