#How to determine a function is isolated or not from Ballerina?

1 messages · Page 1 of 1 (latest)

warm charm
#

I need to know whether a function is isolated or not using Ballerina. How can I do that??

serene flint
#

I don't know whether there are any straightforward way to check this. But, if it is a resource method inside a service, there will be a warning if the method is not isolated. For other methods, you can check it by adding isolated keyword to the function. If it is not an isolated function, there will be a compilation error.

jaunty dagger
#

Can I know the actual requirement why you need to do this?
You can do something like below

import ballerina/io;

public function main() {
    io:println(isIsolated(foo));
}

isolated function foo() {
}

function isIsolated(any func) returns boolean {
    return func is isolated function;
}

Please note that this check will be true for functions that are inferred as isolated as well.

jaunty dagger
#

Looping in @modern saffron as well

modern saffron
#

isolated is part of the type, so as Chiran suggested you can check for is isolated function.

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);
}
warm charm
#

Thanks...It works.. 🙂 ... My requirement is that.. I have a function classify which takes another function as argument. This classify function should do something based on if the argument function is public/private , if the argument function is isolated, if the argument function match with given signature, if the argument function has the specific annotation content... How can I handle these scenarios in Ballerina??? @modern saffron @jaunty dagger

modern saffron
#

You can check for isolated and the signature (again using the is check - fixed though - e.g., f is isolated function(int i) returns error?) and annotations using the annotation access expression (can access only function annotations in Ballerina code, not of the params of the argument function).

You can't check for visibility qualifiers (public, private) in Ballerina though. They are generally not part of the type (they only affect object subtyping).

warm charm
#

Thanks...🙂

warm charm
#

@modern saffron @jaunty dagger functions are converted as isolated functions internally if I don't specify the public . Is it expected???

#

function f1() returns int => 1;
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);
}``` output is true true true