Swan Lake Beta2 is here.
import ballerina/io;

type R record {
    int v;
};

// The initialization expression of an `isolated` variable
// has to be an `isolated` expression, which itself will be
// an `isolated` root.
isolated R r = {v: 0};

isolated function setGlobal(int n) {
    // An `isolated` variable can be accessed within
    // a `lock` statement.
    lock {
        r.v = n;
    }

}

public function main() {
    setGlobal(200);
    // Accesses the `isolated` variable within a
    // `lock` statement.
    lock {
       io:println(r);
    }

}

Combining Isolated Functions and Lock

Combining isolated functions and lock allows isolated functions to use lock to access mutable module-level state. Key concept is isolated root. A value r is an isolated root if mutable state reachable from r cannot be reached from outside except through r. An expression is an isolated expression if it follows rules that guarantee that its value will be an isolated root. e.g.,

  • an expression with a type that is a subtype of readonly is always isolated
  • an expression [E1, E2] is isolated if E1 and E2 are isolated
  • an expression f(E1, E2) is isolated if E1 and E1 are isolated, and the type of f is an isolated function.

import ballerina/io;
type R record {
    int v;
};
isolated R r = {v: 0};

The initialization expression of an isolated variable has to be an isolated expression, which itself will be an isolated root.

isolated function setGlobal(int n) {
    lock {
        r.v = n;
    }

An isolated variable can be accessed within a lock statement.

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

Accesses the isolated variable within a lock statement.

}
bal run combining_isolated_functions_and_lock.bal
{"v":200}
import ballerina/io;

type R record {
    int v;
};

// The initialization expression of an `isolated` variable
// has to be an `isolated` expression, which itself will be
// an `isolated` root.
isolated R r = {v: 0};

isolated function setGlobal(int n) {
    // An `isolated` variable can be accessed within
    // a `lock` statement.
    lock {
        r.v = n;
    }

}

public function main() {
    setGlobal(200);
    // Accesses the `isolated` variable within a
    // `lock` statement.
    lock {
       io:println(r);
    }

}

Combining Isolated Functions and Lock

Combining isolated functions and lock allows isolated functions to use lock to access mutable module-level state. Key concept is isolated root. A value r is an isolated root if mutable state reachable from r cannot be reached from outside except through r. An expression is an isolated expression if it follows rules that guarantee that its value will be an isolated root. e.g.,

  • an expression with a type that is a subtype of readonly is always isolated
  • an expression [E1, E2] is isolated if E1 and E2 are isolated
  • an expression f(E1, E2) is isolated if E1 and E1 are isolated, and the type of f is an isolated function.

import ballerina/io;
type R record {
    int v;
};
isolated R r = {v: 0};

The initialization expression of an isolated variable has to be an isolated expression, which itself will be an isolated root.

isolated function setGlobal(int n) {
    lock {
        r.v = n;
    }

An isolated variable can be accessed within a lock statement.

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

Accesses the isolated variable within a lock statement.

}
bal run combining_isolated_functions_and_lock.bal
{"v":200}

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