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