kotlin - 内联扩展函数 (let/run/also/...) 生成未使用的局部变量

标签 kotlin

使用 Kotlin 1.4-rc .
对于此代码块:

try {
   socket.getOutputStream().let {
      it.write(bytes)
      it.flush()
   }
} catch (e: IOException) {
   ...
}
反编译结果为:
try {
   OutputStream var2 = this.socket.getOutputStream();
   boolean var3 = false;
   boolean var4 = false;
   int var6 = false;
   var2.write(bytes);
   var2.flush();
} catch (IOException var7) {
   ...
}
那么,为什么是
boolean var3 = false;
boolean var4 = false;
生成的,它们的目的是什么,因为它们没有被使用?

最佳答案

见评论 YouTrack here .

These booleans are used by debugger to determine whether we are inside inline function/lambda or not. Unfortunately, println is @InlineOnly, so, you would not see the names of the variables (@InlineOnly functions do not have local variables table and linenumber info, although this is going to change). However, if you create your own inline function and then use it, you will variables like $i$f$ and $i$a$ in LVT (to see the LVT, pass -l flag to javap). The value of these variables is not important (always false, although it can be changed in the future), but range of these variables is what the debugger is looking at. If we are inside $i$f... range, we are inside inline function, if we are inside $i$a... range, we are inside inline lambda.

关于kotlin - 内联扩展函数 (let/run/also/...) 生成未使用的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63246398/

相关文章:

android - Android-从代码更改微调框的字体大小

java - 在 ARCore 中检测垂直平面

android - 在 Kotlin 中,当变量与 lambda 接收器中的字段同名时,如何从作用域函数外部引用变量

android - 使用 PdfRenderer 在 Jetpack Compose 中创建 PDF 查看器

android - Kotlin 中的 Listener 和 High-Order 函数有什么区别?

android - Dagger2 服务中的 RuntimeException AndroidInjection.inject

kotlin - java.lang.NoClassDefFoundError : com/fasterxml/jackson/databind/JsonDeserializer 错误

kotlin - 在哪里可以找到cinterop工具来创建def文件?

android - 如何计算 Glide 图片加载时间?

Kotlin 只读属性,带和不带 getter