智能转换为“X”是不可能的,因为“X”是在不同模块中声明的公共 API 属性

Dan*_*man 6 kotlin

我有一些代码看起来像这样

fun onMessage(message: Message) {
    message.property?.also {
        repository.updateProperty(message.property)
    }
}
Run Code Online (Sandbox Code Playgroud)

其中 的参数updateProperty()不可为空。编译器给出错误:

Smart cast to 'Property' is impossible, because 'Message' is a public API property declared in different module

解决这个问题的最佳解决方案是什么?

Dan*_*man 5

事实证明它就像使用it一样简单。

fun onMessage(message: Message) {
    message.property?.also {
        repository.updateProperty(it)
    }
}
Run Code Online (Sandbox Code Playgroud)