本文介绍了为什么智能广播不能处理这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些结构与此类似的代码:

I have some code with a similar structure to this:

private fun test() : Double {
    val a : Double? = 15.0
    val b : Double? = 20.0
    return if (a == null && b == null) {
        0
    } else if (a != null && b == null) {
        a
    } else if (a == null && b != null) {
        b
    } else {
        a+b
    }
}

根据我的理解,聪明的转换应该推断出在最后的"else"语句中,a和b都不能为null,但似乎并不能推断出这会导致错误.

From my understanding, smart casting should infer that in the final 'else' statement, neither a nor b can be null, yet it doesn't seem to infer this which results in an error.

是否有合理的理由认为智能广播无法按我期望的方式工作,或者它不像我想的那样聪明?

Is there a legitimate reason that smartcasting isn't working in the way I expect here, or is it just not as smart as I think it is?

推荐答案

它并没有您想象的那么聪明.

It just isn't as smart as you think it is.

在IntelliJ IDEA的问题跟踪系统中,有很多与智能转换相关的问题.例如 https://youtrack.jetbrains.com/issue/KT-6822 这是一个子任务的 https://youtrack.jetbrains.com/issue/KT-2454 列出了其他可能相关的问题.我将仔细研究这些问题,看看是否已经跟踪到您的问题,如果没有,请为此创建一个并查看结果.

There are a slew of smart cast related issues in IntelliJ IDEA's issue tracking system. e.g. https://youtrack.jetbrains.com/issue/KT-6822 which is a subtask of https://youtrack.jetbrains.com/issue/KT-2454 which lists other possibly related issues. I would go through those and see if your issue is already tracked or not and, if not, creating one for this and see what comes of it.

这篇关于为什么智能广播不能处理这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:37