在 kotlin 中使用 'let' 时,我的变量名和 'it' 有什么区别?

What is the difference between my variable name and 'it' when using 'let' in kotlin?

在下面的代码片段中,两个Codeblocks有什么区别?
如果我检查 println(name==it),那 returns 是真的,那么他们一定引用了同一个对象,对吧?

然而,当 name 变量有一个值时,一切正常,但是一旦我将它设置为 null,Codeblock 1 就可以工作(意思是,let 块根本不会被执行),但 Codeblock 2 会抛出错误。当 name 为空时,为什么 kotlin 不只是 skip/ignore 代码块 2?

fun main() {
    
    var name:String? = "Cedric"
    //name = null
    
    //Codeblock 1
    name?.let{
        println("The length of name is ${name.length}")
    }
    
    //Codeblock 1
    name?.let{
        println("The length of name is ${it.length}")
    }
    
}

非常感谢,如有任何帮助,我们将不胜感激。

代码块 2 实际上给出了 compile-time 错误,而代码块 1 则没有。我认为这只是一个奇怪的 linting 错误,因为这两种情况应该被同等对待。

据我了解,您发现了编译器中的错误。我在 Android studio 和 IntelliJ 中有两种不同的行为。 如果编译时,编译后的代码被优化,反编译时看起来像这样:

   public final void main() {
      Object name = "Cedric";
      name = (String)null;
   }

万一它没有编译我得到一个错误:

org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate expression: KtNameReferenceExpression
File being compiled: (12,45) in D:/Projekti/kotlin playground/src/Main.kt
The root cause java.lang.AssertionError was thrown at: org.jetbrains.kotlin.codegen.ExpressionCodegen.visitSimpleNameExpression(ExpressionCodegen.java:1797)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:326)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:286)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitDotQualifiedExpression(ExpressionCodegen.java:3258)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitDotQualifiedExpression(ExpressionCodegen.java:112)
    at org.jetbrains.kotlin.psi.KtDotQualifiedExpression.accept(KtDotQualifiedExpression.kt:32)
    at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:31)
    at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:22)
    at org.jetbrains.kotlin.psi.KtVisitor.visitExpression(KtVisitor.java:182)
    at org.jetbrains.kotlin.psi.KtVisitor.visitQualifiedExpression(KtVisitor.java:286)
    at org.jetbrains.kotlin.psi.KtVisitor.visitDotQualifiedExpression(KtVisitor.java:302)
    at org.jetbrains.kotlin.psi.KtDotQualifiedExpression.accept(KtDotQualifiedExpression.kt:32)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:304)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:409)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:366)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeAppend(ExpressionCodegen.java:4108)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeAppendForEntries(ExpressionCodegen.java:874)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.lambda$visitStringTemplateExpression(ExpressionCodegen.java:864)
    at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:122)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:115)
    at org.jetbrains.kotlin.codegen.CallGenerator$DefaultCallGenerator.genValueAndPut(CallGenerator.kt:71)
    at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generateExpression(CallBasedArgumentGenerator.kt:42)
    at org.jetbrains.kotlin.codegen.ArgumentGenerator.generate(ArgumentGenerator.kt:68)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2534)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2506)
    at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments.invoke(Callable.kt:41)
    at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments.invoke(Callable.kt:13)
    at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
    at org.jetbrains.kotlin.codegen.StackValueWithLeaveTask.putSelector(StackValue.kt:67)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:122)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:115)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:386)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:367)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1731)
    at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:64)
    at org.jetbrains.kotlin.codegen.ClosureGenerationStrategy.doGenerateBody(ClosureGenerationStrategy.kt:35)
    at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:84)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:654)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:479)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:261)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:177)
    at org.jetbrains.kotlin.codegen.ClosureCodegen.generateClosureBody(ClosureCodegen.java:181)
    at org.jetbrains.kotlin.codegen.ClosureCodegen.generateBody(ClosureCodegen.java:169)
    at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:129)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1038)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1008)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:999)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:112)
    at org.jetbrains.kotlin.psi.KtLambdaExpression.accept(KtLambdaExpression.java:40)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:304)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:286)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:358)
    at org.jetbrains.kotlin.codegen.CallGenerator$DefaultCallGenerator.genValueAndPut(CallGenerator.kt:70)
    at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generateExpression(CallBasedArgumentGenerator.kt:42)
    at org.jetbrains.kotlin.codegen.ArgumentGenerator.generate(ArgumentGenerator.kt:68)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2534)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2506)
    at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments.invoke(Callable.kt:41)
    at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments.invoke(Callable.kt:13)
    at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
    at org.jetbrains.kotlin.codegen.CoercionValue.putSelector(StackValue.kt:31)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:122)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:115)
    at org.jetbrains.kotlin.codegen.StackValue$StackValueWithSimpleReceiver.putReceiver(StackValue.java:2089)
    at org.jetbrains.kotlin.codegen.StackValueWithLeaveTask.putReceiver(StackValue.kt:63)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:120)
    at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:115)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:386)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:367)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1731)
    at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:64)
    at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:84)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:654)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:479)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:261)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:177)
    at org.jetbrains.kotlin.codegen.FunctionCodegen.gen(FunctionCodegen.java:148)
    at org.jetbrains.kotlin.codegen.MemberCodegen.genSimpleMember(MemberCodegen.java:197)
    at org.jetbrains.kotlin.codegen.PackagePartCodegen.generateBody(PackagePartCodegen.java:95)
    at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:129)
    at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:145)
    at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:70)
    at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generatePackage(CodegenFactory.kt:88)
    at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generateModule(CodegenFactory.kt:67)
    at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
    at org.jetbrains.kotlin.idea.internal.KotlinBytecodeToolWindow$Companion.compileSingleFile(KotlinBytecodeToolWindow.kt:335)
    at org.jetbrains.kotlin.idea.internal.KotlinBytecodeToolWindow$Companion.getBytecodeForFile(KotlinBytecodeToolWindow.kt:248)
    at org.jetbrains.kotlin.idea.internal.KotlinBytecodeToolWindow$UpdateBytecodeToolWindowTask.processRequest(KotlinBytecodeToolWindow.kt:127)
    at org.jetbrains.kotlin.idea.internal.KotlinBytecodeToolWindow$UpdateBytecodeToolWindowTask.processRequest(KotlinBytecodeToolWindow.kt:74)
    at org.jetbrains.kotlin.idea.util.LongRunningReadTask.run(LongRunningReadTask.java:115)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:869)
    at org.jetbrains.kotlin.idea.util.LongRunningReadTask.lambda$runWithWriteActionPriority[=11=](LongRunningReadTask.java:235)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess(CoreProgressManager.java:166)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:627)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:572)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:61)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:153)
    at org.jetbrains.kotlin.idea.util.LongRunningReadTask.runWithWriteActionPriority(LongRunningReadTask.java:235)
    at org.jetbrains.kotlin.idea.util.LongRunningReadTask.run(LongRunningReadTask.java:110)
    at com.intellij.util.RunnableCallable.call(RunnableCallable.java:20)
    at com.intellij.util.RunnableCallable.call(RunnableCallable.java:11)
    at com.intellij.openapi.application.impl.ApplicationImpl.call(ApplicationImpl.java:255)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.AssertionError: Couldn't find descriptor for 'length'
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitSimpleNameExpression(ExpressionCodegen.java:1797)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitSimpleNameExpression(ExpressionCodegen.java:112)
    at org.jetbrains.kotlin.psi.KtNameReferenceExpression.accept(KtNameReferenceExpression.kt:59)
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:304)
    ... 105 more

编译器似乎没有遵循代码优化。因此,即使它应该或不应该编译,也有两种不同的行为,这意味着其中一种是不正确的。