Created
December 31, 2019 10:44
-
-
Save MaryamZi/dc90ff82377986c076f1642627b04da6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/io; | |
const MY_REASON = "MyReason"; | |
type MyDetail record {| | |
string message; | |
error cause?; | |
int code?; | |
anydata|error...; | |
|}; | |
type MyError error<MY_REASON, MyDetail>; | |
function foo() returns error? { | |
return MyError(message = "err message", code = 123, bar = 12.3, baz = false); | |
} | |
public function main() { | |
// Say I call a function returning either an `error` or `()`. | |
error? res = foo(); | |
if res is MyError { | |
string errMessage; | |
int? errCode; | |
map<anydata|error> rest; | |
// Using an indirect error binding pattern. | |
MyError(message = errMessage, code = errCode, ...rest) = res; | |
io:println("errMessage: ", errMessage); // err message | |
io:println("errCode is int: ", errCode is int); // true | |
io:println("length of rest: ", rest.length()); // 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment