Hello
I have a question about how anonymous code blocks are handlied in Kotlin.
When programming in Java, I often use anonymous code blocks as a way of restricting the scope of local variables. This mechanism is used a lot in Java sourcecode that I am generating, and am looking for a Kotlin equivalent.
So, in the following block, I can re-use param1, param2, param3 multiple times.
public static void main (String[] args) {
{
int param1 = myFunc(1);
int param2 = myFunc(2);
int param3 = myFunc(3);
doSomething(param1, param2, param3);
}
{
int param1 = myFunc(4);
int param2 = myFunc(5);
int param3 = myFunc(6);
doSomething(param1, param2, param3);
} {
int param1 = myFunc(7);
int param2 = myFunc(8);
int param3 = myFunc(9);
doSomething(param1, param2, param3);
}
}
Now, when I try this in Kotlin (I wonāt post the Kotlin snippet as itās so trivial) , I receive the following messageā¦
Multiple markers at this line - The lambda expression is unused. If you mean a block, you can use ārun { ā¦ }ā - 1 **
** changed line
This sounds like itās calling the block as an anonymous function, which has two problems. One is that I get a lot of warning everywhere, and two is that I imagine that the performance is much lower than in native java code.
What I would like to know is, is there a Kotlin equivilent of local scoping / anonymous code blocks that has an identical performance profile. And, will it remove the yellow warning markers from my codeā¦