为什么在 Assert.assertNotNull 内调用 void 函数是有效的?
Why is it valid to call a void function within Assert.assertNotNull?
以下面的伪代码为例,
Assert.assertNotNull(cut.func())
其中 cut
是 non-null class 待测,func()
是 returns void 的某个函数.
此外,假设 func()
不会引发任何错误,这将导致 valid 断言。
既然isn't possible to set an object to be null from within itself in Java / Kotlin,(即func()
不能设置cut
为null
),为什么在non内调用void函数是有效的-null 断言?
仅仅是因为void
和null
不一样吗?
Java的void
return类型映射到Kotlin中的Unit
对象,而Unit
不是null
。
val x = System.out.println("") // void Java method
println(x == null) // "false"
println(x == Unit) // "true"
以下面的伪代码为例,
Assert.assertNotNull(cut.func())
其中 cut
是 non-null class 待测,func()
是 returns void 的某个函数.
此外,假设 func()
不会引发任何错误,这将导致 valid 断言。
既然isn't possible to set an object to be null from within itself in Java / Kotlin,(即func()
不能设置cut
为null
),为什么在non内调用void函数是有效的-null 断言?
仅仅是因为void
和null
不一样吗?
Java的void
return类型映射到Kotlin中的Unit
对象,而Unit
不是null
。
val x = System.out.println("") // void Java method
println(x == null) // "false"
println(x == Unit) // "true"