Last active
December 31, 2019 11:22
-
-
Save MaryamZi/43a4654292abb5bfd4f9429c9da9e197 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?; | |
boolean|float...; | |
|}; | |
type MyError error<MY_REASON, MyDetail>; | |
function abc() returns error { | |
return error("ErrorReason", message = "err message", code = 123, bar = 12.3); | |
} | |
function def() returns MyError { | |
return MyError(message = "my message", code = 456, bar = 100.4, baz = false); | |
} | |
public function main() { | |
// The following typed binding patterns define the variables and set the | |
// destructured values to them. | |
// Since `abc()` returns a value of type `error` the variables are defined | |
// with the following types. | |
// - `reason` - `string` since a reason is always present and is a string. | |
// - `errMessage` - `string?` since `message` is optional, but is `string` if present | |
// - `errCode` - `anydata|error` since `code` is part of the rest field if present | |
// - `rest` - `map<anydata|error>` | |
error error(reason, message = errMessage, code = errCode, ...rest) = abc(); | |
io:println("reason: ", reason); // ErrorReason | |
io:println("errMessage: ", errMessage); // err message | |
// The following is valid since `errMessage` is defined to be of type `string?` | |
string? message = errMessage; | |
io:println("errCode: ", errCode); // 123 | |
io:println("length of rest: ", rest.length()); // 1 - bar | |
// Since `def()` returns a value of type `MyError` the variables are defined | |
// with the following types. | |
// - `errMessage2` - `string` since `message` is a mandatory `string` field | |
// - `errCode2` - `int?` since `code` is optional, but is `int` if present | |
// - `rest2` - `map<boolean|float|error>` - `boolean|float` for the rest field | |
// and `error` for `cause` that is optional, but error if present | |
MyError MyError(message = errMessage2, code = errCode2, ...rest2) = def(); | |
io:println("errMessage2: ", errMessage2); // my message | |
io:println("errCode2 is int: ", errCode2 is int); // true | |
// The following is valid since `errCode2` is defined to be of type `int?` | |
int? code = errCode2; | |
io:println("length of rest: ", rest2.length()); // 2 | |
// The following is valid since `rest2` is defined to be of type `map<boolean|float|error>` | |
map<boolean|float|error> restMap = rest2; | |
// `var` is used for inferring, and since `def()` returns `MyError` the type is inferred | |
// as `MyError`. | |
var error(reason3, message = errMessage3, code = errCode3, ...rest3) = def(); | |
io:println("errMessage3: ", errMessage3); // my message | |
io:println("errCode3 is int: ", errCode3 is int); // true | |
// The following is valid since `errCode3` is defined to be of type `int?` | |
int? code3 = errCode3; | |
io:println("length of rest: ", rest3.length()); // 2 | |
// The following is valid since `rest3` is defined to be of type `map<boolean|float|error>` | |
map<boolean|float|error> restMap2 = rest3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment