Swan Lake Beta3 is here.
import ballerina/io;

type R record {
    int v;
};

final int N = getN();

function getN() returns int {
    return 100;
}

// Can access mutable state that is passed as a parameter.
isolated function set(R r) returns R {
    // Can access non-`isolated` module-level state only if the variable
    // is `final` and the type is a subtype of `readonly` or
    // `isolated object {}`.
    r.v = N;

    return r;
}

R r = {v: 0};

// This is not an `isolated` function.
function setGlobal(int n) {

    r.v = n;
}

public function main() {
    setGlobal(200);
    io:println(r);
    io:println(set(r));
}

Isolated Functions

A call to an isolated function is concurrency-safe if it is called with arguments that are safe at least until the call returns.

A function defined as isolated:

  • has access to mutable state only through its parameters
  • has unrestricted access to immutable state
  • can only call functions that are isolated


Constraints are enforced at compile-time. isolated is a part of the function type. Weaker concept than pure function.

import ballerina/io;
type R record {
    int v;
};
final int N = getN();
function getN() returns int {
    return 100;
}
isolated function set(R r) returns R {

Can access mutable state that is passed as a parameter.

    r.v = N;

Can access non-isolated module-level state only if the variable is final and the type is a subtype of readonly or isolated object {}.

    return r;
}
R r = {v: 0};
function setGlobal(int n) {

This is not an isolated function.

    r.v = n;
}
public function main() {
    setGlobal(200);
    io:println(r);
    io:println(set(r));
}
bal run isolated_functions.bal
{"v":200}
{"v":100}
import ballerina/io;

type R record {
    int v;
};

final int N = getN();

function getN() returns int {
    return 100;
}

// Can access mutable state that is passed as a parameter.
isolated function set(R r) returns R {
    // Can access non-`isolated` module-level state only if the variable
    // is `final` and the type is a subtype of `readonly` or
    // `isolated object {}`.
    r.v = N;

    return r;
}

R r = {v: 0};

// This is not an `isolated` function.
function setGlobal(int n) {

    r.v = n;
}

public function main() {
    setGlobal(200);
    io:println(r);
    io:println(set(r));
}

Isolated Functions

A call to an isolated function is concurrency-safe if it is called with arguments that are safe at least until the call returns.

A function defined as isolated:

  • has access to mutable state only through its parameters
  • has unrestricted access to immutable state
  • can only call functions that are isolated


Constraints are enforced at compile-time. isolated is a part of the function type. Weaker concept than pure function.

import ballerina/io;
type R record {
    int v;
};
final int N = getN();
function getN() returns int {
    return 100;
}
isolated function set(R r) returns R {

Can access mutable state that is passed as a parameter.

    r.v = N;

Can access non-isolated module-level state only if the variable is final and the type is a subtype of readonly or isolated object {}.

    return r;
}
R r = {v: 0};
function setGlobal(int n) {

This is not an isolated function.

    r.v = n;
}
public function main() {
    setGlobal(200);
    io:println(r);
    io:println(set(r));
}
bal run isolated_functions.bal
{"v":200}
{"v":100}
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