#Isolated filter function

1 messages · Page 1 of 1 (latest)

night lion
#

Hi team,

I am trying to do a following type of operation using filter operation inside an isolated method and it gives the following error. How can I fix this?

int[] numbers = [12, 43, 60, 75, 10];
int[] filteredNumber = numbers.filter(n => n > 50);

Error - incompatible types: expected an 'isolated' function

autumn nest
#

Is this the exact sample? Seems to work for me.

public isolated function main() {
    int[] numbers = [12, 43, 60, 75, 10];
    int[] filteredNumber = numbers.filter(n => n > 50);
}
toxic dew
#

Yes, it works for me as well. What is the Ballerina version you are using?

night lion
#

Sorry I have attached the sample. Following is the sample code.

public isolated someFunc() returns error? {
 User[] users = check getUsers(params);
 User[] filteredUsers = users.filter(user => user.orgName == repoOwner);
}
autumn nest
#

Not sure where repoOwner is defined, but may be because it is not a final variable.

#

What you're expecting is equivalent to

User[] filteredUsers = users.filter(isolated function (User user) returns boolean {
    return user.orgName == repoOwner;
});

for which you will get an error at repoOwner, because it is a variable defined outside the function and is not implicitly/explicitly final. Similar to a normal isolated function.

night lion
#

@autumn nest Thank you. I have used this. Can we use the shorten syntax with isolated.