Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    // Allowed only if the return value is `()`.
    doX();

    // Allowed if the return value does not include an `error`.
    _ = getX();

    // Use `checkpanic` if you don't want to handle an `error`.
    checkpanic tryX(true);
    checkpanic tryX(false);

}

function doX() {
    io:println("Do X");
}

function getX() returns boolean {
    io:println("Get X");
    return true;
}

function tryX(boolean x) returns error? {
    io:println("Try X");
    if !x {
        return error("error!");
    }
}

Ignoring Return Values and Errors

Ballerina does not allow silently ignoring return values. To ignore a return value, assign it to _; this is like an implicitly declared variable that cannot be referenced. When a return type includes an error, you have to do something with the error. _ is of type any: you cannot use _ to ignore an error. checkpanic is like check, but panics on error rather than returning.

import ballerina/io;
public function main() {
    doX();

Allowed only if the return value is ().

    _ = getX();

Allowed if the return value does not include an error.

    checkpanic tryX(true);
    checkpanic tryX(false);

Use checkpanic if you don’t want to handle an error.

}
function doX() {
    io:println("Do X");
}
function getX() returns boolean {
    io:println("Get X");
    return true;
}
function tryX(boolean x) returns error? {
    io:println("Try X");
    if !x {
        return error("error!");
    }
}
bal run ignoring_return_values_and_errors.bal
Do X
Get X
Try X
Try X
error: error!
        at ignoring_return_values_and_errors:tryX(ignoring_return_values_and_errors.bal:28)
           ignoring_return_values_and_errors:main(ignoring_return_values_and_errors.bal:12)
import ballerina/io;

public function main() {
    // Allowed only if the return value is `()`.
    doX();

    // Allowed if the return value does not include an `error`.
    _ = getX();

    // Use `checkpanic` if you don't want to handle an `error`.
    checkpanic tryX(true);
    checkpanic tryX(false);

}

function doX() {
    io:println("Do X");
}

function getX() returns boolean {
    io:println("Get X");
    return true;
}

function tryX(boolean x) returns error? {
    io:println("Try X");
    if !x {
        return error("error!");
    }
}

Ignoring Return Values and Errors

Ballerina does not allow silently ignoring return values. To ignore a return value, assign it to _; this is like an implicitly declared variable that cannot be referenced. When a return type includes an error, you have to do something with the error. _ is of type any: you cannot use _ to ignore an error. checkpanic is like check, but panics on error rather than returning.

import ballerina/io;
public function main() {
    doX();

Allowed only if the return value is ().

    _ = getX();

Allowed if the return value does not include an error.

    checkpanic tryX(true);
    checkpanic tryX(false);

Use checkpanic if you don’t want to handle an error.

}
function doX() {
    io:println("Do X");
}
function getX() returns boolean {
    io:println("Get X");
    return true;
}
function tryX(boolean x) returns error? {
    io:println("Try X");
    if !x {
        return error("error!");
    }
}
bal run ignoring_return_values_and_errors.bal
Do X
Get X
Try X
Try X
error: error!
        at ignoring_return_values_and_errors:tryX(ignoring_return_values_and_errors.bal:28)
           ignoring_return_values_and_errors:main(ignoring_return_values_and_errors.bal:12)

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