Why does ide say "Value captured in a closure" when using Iterable.flatMap but doesn't when using map then flatten in kotlin? - TagMerge
2Why does ide say "Value captured in a closure" when using Iterable.flatMap but doesn't when using map then flatten in kotlin?Why does ide say "Value captured in a closure" when using Iterable.flatMap but doesn't when using map then flatten in kotlin?

Why does ide say "Value captured in a closure" when using Iterable.flatMap but doesn't when using map then flatten in kotlin?

Asked 12 months ago
0
2 answers

If the fun wasn't inline then in both cases queryAction would have to be captured in a closure, because it's coming from outside of the lambda. It means that a class which is generated under the hood holds a reference to the variable, and when the lambda is eventually executed, it uses that reference.

But here, it's inlined just fine. Idea tries it's best to give information about captured variables but in this situation it might be a limitation of Idea's static analysis. The first message is just incorrect.

As @broot suggested, you can replace .map { f(it) } with just .map(f).

Source: link

0

Compare these two code samples:
// without syntax highlighting

public class NiceDay {
  public static void main(String[] args) {
    System.out.println("It's a nice day out!");
  }
}
// without syntax highlighting public class NiceDay { public static void main(String[] args) { System.out.println("It's a nice day out!"); } }
// with syntax highlighting
public class NiceDay {  public static void main(String[] args) {    System.out.println("It's a nice day out!");  }}
// without syntax highlighting public class NiceDay { public static void main(String[] args) { System.out.println("It's a nice day out!"); } }
// with syntax highlighting
public class NiceDay {  public static void main(String[] args) {    System.out.println("It's a nice day out!");  }}

Source: link

Recent Questions on kotlin

    Programming Languages