Swan Lake Beta2 is here.
import ballerina/io;

// An `isolated` object’s mutable state is `isolated` from the
// rest of the program.
isolated class Counter {
    // `n` is a mutable field.
    private int n = 0;

    isolated function get() returns int {
        lock {
            // `n` can only be accessed using `self`.
            return self.n;

        }
    }

    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}

public function main() {
    // The object’s mutable state is accessible only via the
    // object itself making it an “isolated root”.
    Counter c = new;

    c.inc();
    int v = c.get();
    io:println(v);
}

Isolated Objects

An object defined as isolated is similar to a module with isolated module-level variables. Mutable fields of an isolated object,

  • must be private and so can only be accessed using self
  • must be initialized with an isolated expression
  • must only be accessed within a lock statement
  • lock statement must follow the same rules for self as for an isolated variable
  • field is mutable unless it is final and has type that is subtype of readonly


Isolated root concept treats isolated objects as opaque. Isolated functions can access a final variable whose type is an isolated object.

import ballerina/io;
isolated class Counter {

An isolated object’s mutable state is isolated from the rest of the program.

    private int n = 0;

n is a mutable field.

    isolated function get() returns int {
        lock {
            return self.n;

n can only be accessed using self.

        }
    }
    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}
public function main() {
    Counter c = new;

The object’s mutable state is accessible only via the object itself making it an “isolated root”.

    c.inc();
    int v = c.get();
    io:println(v);
}
bal run isolated_objects.bal
1
import ballerina/io;

// An `isolated` object’s mutable state is `isolated` from the
// rest of the program.
isolated class Counter {
    // `n` is a mutable field.
    private int n = 0;

    isolated function get() returns int {
        lock {
            // `n` can only be accessed using `self`.
            return self.n;

        }
    }

    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}

public function main() {
    // The object’s mutable state is accessible only via the
    // object itself making it an “isolated root”.
    Counter c = new;

    c.inc();
    int v = c.get();
    io:println(v);
}

Isolated Objects

An object defined as isolated is similar to a module with isolated module-level variables. Mutable fields of an isolated object,

  • must be private and so can only be accessed using self
  • must be initialized with an isolated expression
  • must only be accessed within a lock statement
  • lock statement must follow the same rules for self as for an isolated variable
  • field is mutable unless it is final and has type that is subtype of readonly


Isolated root concept treats isolated objects as opaque. Isolated functions can access a final variable whose type is an isolated object.

import ballerina/io;
isolated class Counter {

An isolated object’s mutable state is isolated from the rest of the program.

    private int n = 0;

n is a mutable field.

    isolated function get() returns int {
        lock {
            return self.n;

n can only be accessed using self.

        }
    }
    isolated function inc() {
        lock {
            self.n += 1;
        }
    }
}
public function main() {
    Counter c = new;

The object’s mutable state is accessible only via the object itself making it an “isolated root”.

    c.inc();
    int v = c.get();
    io:println(v);
}
bal run isolated_objects.bal
1

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