Swan Lake Beta3 is here.
import ballerina/io;

function demo(string s) returns int|error {
    // Named workers have a return type, which defaults to nil
    // if not specified.
    worker A returns int|error {
        // A return statement in a named worker terminates 
        // the worker, not the function.
        // Similarly, when `check` is used and the expression evaluates
        // to an error, the error value is returned terminating only the worker.
        int x = check int:fromString(s);
        return x + 1;

    }

    io:println("In function worker");

    // Waiting on a named worker will give its return value.
    int y = check wait A;

    return y + 1;
}

public function main() returns error? {
    int res = check demo("50");
    io:println(res);

    res = check demo("50m");
    io:println(res);
}

Named Worker Return Values

Named workers have a return type, which defaults to nil. A return statement in a named worker terminates the worker, not the function. Similarly, when check is used and the expression evaluates to an error, the error value is returned terminating the worker. Waiting on a named worker will give its return value.

import ballerina/io;
function demo(string s) returns int|error {
    worker A returns int|error {

Named workers have a return type, which defaults to nil if not specified.

        int x = check int:fromString(s);
        return x + 1;

A return statement in a named worker terminates the worker, not the function. Similarly, when check is used and the expression evaluates to an error, the error value is returned terminating only the worker.

    }
    io:println("In function worker");
    int y = check wait A;

Waiting on a named worker will give its return value.

    return y + 1;
}
public function main() returns error? {
    int res = check demo("50");
    io:println(res);
    res = check demo("50m");
    io:println(res);
}
bal run named_worker_return_values.bal
In function worker
52
In function worker
error: {ballerina/lang.int}NumberParsingError {"message":"'string' value '50m' cannot be converted to 'int'"}
import ballerina/io;

function demo(string s) returns int|error {
    // Named workers have a return type, which defaults to nil
    // if not specified.
    worker A returns int|error {
        // A return statement in a named worker terminates 
        // the worker, not the function.
        // Similarly, when `check` is used and the expression evaluates
        // to an error, the error value is returned terminating only the worker.
        int x = check int:fromString(s);
        return x + 1;

    }

    io:println("In function worker");

    // Waiting on a named worker will give its return value.
    int y = check wait A;

    return y + 1;
}

public function main() returns error? {
    int res = check demo("50");
    io:println(res);

    res = check demo("50m");
    io:println(res);
}

Named Worker Return Values

Named workers have a return type, which defaults to nil. A return statement in a named worker terminates the worker, not the function. Similarly, when check is used and the expression evaluates to an error, the error value is returned terminating the worker. Waiting on a named worker will give its return value.

import ballerina/io;
function demo(string s) returns int|error {
    worker A returns int|error {

Named workers have a return type, which defaults to nil if not specified.

        int x = check int:fromString(s);
        return x + 1;

A return statement in a named worker terminates the worker, not the function. Similarly, when check is used and the expression evaluates to an error, the error value is returned terminating only the worker.

    }
    io:println("In function worker");
    int y = check wait A;

Waiting on a named worker will give its return value.

    return y + 1;
}
public function main() returns error? {
    int res = check demo("50");
    io:println(res);
    res = check demo("50m");
    io:println(res);
}
bal run named_worker_return_values.bal
In function worker
52
In function worker
error: {ballerina/lang.int}NumberParsingError {"message":"'string' value '50m' cannot be converted to 'int'"}
Subscribe to the newsletter

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