英文:
How to use map function inside an isolated function in Ballerina
问题
以下是我的代码。VS Code 插件报错,表示在 map 内部使用的函数不是隔离函数。这有什么问题。
class Node {
(int|string)[] path;
隔离函数 init((int|string)[] path) {
self.path = path;
}
隔离函数 getPath() 返回 string[] {
return self.path.clone().map(n => n is int ? "@" : n);
}
}
英文:
Following is my code. VS code plugin complains that the function used inside the map is not an isolated function. What is the problem with it.
class Node {
(int|string)[] path;
isolated function init((int|string)[] path) {
self.path = path;
}
isolated function getPath() returns string[] {
return self.path.clone().'map(n => n is int ? "@" : n);
}
}
答案1
得分: 1
错误似乎是由于参数的类型缩小,推断匿名函数是否为isolated
函数时存在错误。
作为一种解决方法,您需要明确指定该函数是isolated
函数。
class Node {
(int|string)[] path;
isolated function init((int|string)[] path) {
self.path = path;
}
isolated function getPath() returns string[] {
return self.path.map(
isolated function (int|string n) returns string => n is int ? "@" : n);
}
}
英文:
The error here seems to be due to a bug in inferring if the (infer) anonymous function is an isolated
function when there's type narrowing of a parameter.
As a workaround, you would have to explicitly specify that the function is an isolated
function.
class Node {
(int|string)[] path;
isolated function init((int|string)[] path) {
self.path = path;
}
isolated function getPath() returns string[] {
return self.path.map(
isolated function (int|string n) returns string => n is int ? "@" : n);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论