I'm getting Overload resolution ambiguity error on kotlin safe call

后端 未结 5 2183
眼角桃花
眼角桃花 2021-02-20 01:27

I have a nullable string variable ab. If I call toUpperCase via safe call operator after I assign null to it, kotlin gives error.

fun m         


        
5条回答
  •  醉话见心
    2021-02-20 01:59

    I decompiled your function and I figured: after the moment you make ab = null the compiler will smartcast it, putting null (ACONST_NULL) in every ocurrence of ab. Then as null has no type. you can't infer the type for the receiver of toUpperCase().

    This is the java equivalent code generated from the kotlin byte code:

    public final void main(@NotNull String[] args) {
       Intrinsics.checkParameterIsNotNull(args, "args");
       String ab = "hello";
       ab = (String)null;
       Object var3 = null;
       System.out.println(var3);
    }
    

    It looks as an issue that should be resolved by the kotlin team.

提交回复
热议问题