ballerina 学习十三 函数&&documentation

时间:2023-03-10 07:24:39
ballerina 学习十三  函数&&documentation

ballerina 函数和其他语言一样的,可以实现重用

简单例子

  • 代码
import ballerina/io;

documentation {
`User` is a user defined object
F{{name}} This is the description for the User's field `name`.
}
public type User object{
public {
string name;
}
documentation {
`printusername` printusername.
R{{}} This is the description for the `doThatOnObject` function Return
value. `R` doc prefix is used to mark a return value for a function.
}
public function printusername() returns (string){
return name;
}
}; // 简单函数
function printusername (string name) {
io:println(name);
}
// 简单函数,同时包含一个默认参数
function printname (string name , int typeinfo = 0) {
io:println(name,typeinfo);
}
// 函数 rest 参数 function printusernames(string… names) {
foreach name in names {
io:println(name);
}
}
function main (string… args) {
printname("dalong");
printusername("demoapp");
string[] names = ["first", "second", "third"];
printusernames(…names);
}
  • 运行
ballerina doc f.bal
ballerina run f.bal
  • 输出结果
demoapp
dalong