Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    // function's default worker, worker `A` and `B` run concurrently.
    worker A {
        // It is safe to access the parameters of `sayHello` for the
        // duration of the function call.
        string a = sayHello("John");

        io:println(a);
    }

    worker B {
        string b = sayHello("Anne");
        io:println(b);
    }

    // `sayHello` is inferred to be an `isolated` function.
    boolean c = sayHello is isolated function (string str) returns string;

    io:println(c);
}

function sayHello(string name) returns string {
    return "Hello " + name;
}

Inferring Isolated

isolated is a complex feature, which would be a lot for an application developer to understand. A typical Ballerina application consists of a single module that imports multiple library modules. Within a single module, we can infer isolated qualifiers. Object w/o mutable fields is inherently isolated. Application developer’s responsibility is to use lock statement where needed. e.g.,

  • access self in a service object with mutable state
  • access mutable module-level variables


Compiler can inform developer where missing locks are preventing a service object or method from being isolated.

import ballerina/io;
public function main() {
    worker A {

function’s default worker, worker A and B run concurrently.

        string a = sayHello("John");

It is safe to access the parameters of sayHello for the duration of the function call.

        io:println(a);
    }
    worker B {
        string b = sayHello("Anne");
        io:println(b);
    }
    boolean c = sayHello is isolated function (string str) returns string;

sayHello is inferred to be an isolated function.

    io:println(c);
}
function sayHello(string name) returns string {
    return "Hello " + name;
}
bal run inferring_isolated.bal
true
Hello Anne
Hello John
import ballerina/io;

public function main() {
    // function's default worker, worker `A` and `B` run concurrently.
    worker A {
        // It is safe to access the parameters of `sayHello` for the
        // duration of the function call.
        string a = sayHello("John");

        io:println(a);
    }

    worker B {
        string b = sayHello("Anne");
        io:println(b);
    }

    // `sayHello` is inferred to be an `isolated` function.
    boolean c = sayHello is isolated function (string str) returns string;

    io:println(c);
}

function sayHello(string name) returns string {
    return "Hello " + name;
}

Inferring Isolated

isolated is a complex feature, which would be a lot for an application developer to understand. A typical Ballerina application consists of a single module that imports multiple library modules. Within a single module, we can infer isolated qualifiers. Object w/o mutable fields is inherently isolated. Application developer’s responsibility is to use lock statement where needed. e.g.,

  • access self in a service object with mutable state
  • access mutable module-level variables


Compiler can inform developer where missing locks are preventing a service object or method from being isolated.

import ballerina/io;
public function main() {
    worker A {

function’s default worker, worker A and B run concurrently.

        string a = sayHello("John");

It is safe to access the parameters of sayHello for the duration of the function call.

        io:println(a);
    }
    worker B {
        string b = sayHello("Anne");
        io:println(b);
    }
    boolean c = sayHello is isolated function (string str) returns string;

sayHello is inferred to be an isolated function.

    io:println(c);
}
function sayHello(string name) returns string {
    return "Hello " + name;
}
bal run inferring_isolated.bal
true
Hello Anne
Hello John

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