Last active
December 31, 2019 10:31
-
-
Save MaryamZi/8b1720059fc2c9db142784cac210f040 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; | |
function foo() returns error? { | |
return error("ErrReason", message = "err message", code = 123, bar = 45.6, baz = false); | |
} | |
public function main() { | |
error? res = foo(); | |
if res is error { | |
string errReason; | |
string? errMessage; | |
anydata|error errCode; | |
anydata|error errPriority; | |
// Since the unmatched fields could be of any type belonging to `anydata|error`, we use a | |
// `map<anydata|error>` to hold the rest of the fields. | |
map<anydata|error> rest; | |
// Only one rest binding pattern can be specified. | |
// Specified as `...var-name`. | |
error(errReason, message = errMessage, code = errCode, priority = errPriority, ...rest) = res; | |
io:println("errReason: ", errReason); // ErrReason | |
io:println("errMessage: ", errMessage); // err message | |
io:println("errCode is int: ", errCode is int); // true | |
io:println("errPriority is (): ", errPriority is ()); // true | |
io:println("length of rest: ", rest.length()); // 2 - bar and baz | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment