Kotlin可为空的比较:不同的表达式vs函数结果

时间:2019-01-25 11:11:14

标签: debugging kotlin comparison nullable non-nullable

我没有得到以下结果: 我有一个函数alreadyDoneToday():Boolean。在调试时,我注意到一个奇怪的行为。表达式本身返回true,但在函数内包装时返回false

enter image description here

var lastDone: Date? = null
...
fun today(): Date {
    var calendar = Calendar.getInstance()
    return toDateWithoutTime(calendar.time)
}

我现在的解决方案是添加一个空检查:

fun alreadyDoneToday():Boolean{
    return lastDone != null && lastDone!! == today()
}

我有点希望我可以将可为空的对象与不可为空的对象进行比较。显然不是。但是,调试器的输出确实具有误导性。

有任何线索吗?

马蒂亚斯(BR Matthias)

0 个答案:

没有答案