Swan Lake Beta2 is here.
import ballerina/io;

// Convert bytes to a string and then to an int.
function intFromBytes(byte[] bytes) returns int|error {

    // Use `check` with an expression that may return `error`.
    // If `string:fromBytes(bytes)` returns an error, `check`
    // makes the function return the error here.
    // If not, the returned string value is used as the 
    // value of the `str` variable.
    string str = check string:fromBytes(bytes);


    return int:fromString(str);
}

public function main() {
    int|error res = intFromBytes([104, 101, 108, 108, 111]);
    io:println(res);
}

Check Expression

check E is used with expression E that might result in an error. If E does result in an error, then check makes the function return that error immediately. Type of check E does not include error. The control flow remains explicit.

import ballerina/io;
function intFromBytes(byte[] bytes) returns int|error {

Convert bytes to a string and then to an int.

    string str = check string:fromBytes(bytes);

Use check with an expression that may return error. If string:fromBytes(bytes) returns an error, check makes the function return the error here. If not, the returned string value is used as the value of the str variable.

    return int:fromString(str);
}
public function main() {
    int|error res = intFromBytes([104, 101, 108, 108, 111]);
    io:println(res);
}
bal run check_expression.bal
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'")
import ballerina/io;

// Convert bytes to a string and then to an int.
function intFromBytes(byte[] bytes) returns int|error {

    // Use `check` with an expression that may return `error`.
    // If `string:fromBytes(bytes)` returns an error, `check`
    // makes the function return the error here.
    // If not, the returned string value is used as the 
    // value of the `str` variable.
    string str = check string:fromBytes(bytes);


    return int:fromString(str);
}

public function main() {
    int|error res = intFromBytes([104, 101, 108, 108, 111]);
    io:println(res);
}

Check Expression

check E is used with expression E that might result in an error. If E does result in an error, then check makes the function return that error immediately. Type of check E does not include error. The control flow remains explicit.

import ballerina/io;
function intFromBytes(byte[] bytes) returns int|error {

Convert bytes to a string and then to an int.

    string str = check string:fromBytes(bytes);

Use check with an expression that may return error. If string:fromBytes(bytes) returns an error, check makes the function return the error here. If not, the returned string value is used as the value of the str variable.

    return int:fromString(str);
}
public function main() {
    int|error res = intFromBytes([104, 101, 108, 108, 111]);
    io:println(res);
}
bal run check_expression.bal
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'")

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand