Search code examples
ballerinaisolationballerina-swan-lake

How to programatically determine if a function is isolated or not in Ballerina?


In my Ballerina code, I need to validate whether a given Ballerina function is isolated within my logic. What would be the recommended way to do that?


Solution

  • isolated is part of the type. you can check for is isolated function as follows.

    import ballerina/io;
    
    public function f1() returns int => 1;
    public isolated function f2() returns int => 1;
    
    public function main() {
        boolean isolatedFn = f1 is isolated function () returns int;
        io:println(isolatedFn);
    
        // should be possible, but not allowed atm, param/return types have to match
        io:println(f1 is isolated function);
    
        function f = f1;
        io:println(f is isolated function);
        io:println(<function> f2 is isolated function);
    }
    

    Please note that this check will be true for functions that are inferred as isolated(https://ballerina.io/learn/by-example/inferring-isolated/) as well.