java如何验证kotlin中的数据类是否为null
我有一个数据类(如下所示)
data class Request(val containerType: String,
val containerId: String)
在下面给出的另一个函数中,我将其作为参数调用
fun someLogicalFunction(request: Request) {
// validate if request is not null here
if(request == null) { // i know this is wrong, what can be done for this?
// do something
} else { // do something }
}
如何直接在kotlin中检查请求是否为空
# 1 楼答案
someLogicalFunction
的参数类型是不可为空的(与Request
的属性相同),因此如果从Kotlin代码调用它,您可以在编译时保证不会给出空值,并且不需要进行空检查然而,如果你真的想/需要允许空值,我建议这样做
显然,用对任何需要都有意义的东西来替换
isValid
实现