例如,如果我有以下数据类:
data class Data(
val name: String = "",
val number: Long = 0
)
以及可以返回null
的函数:
fun newName(): String? {}
fun newNumber(): Long? {}
I know I can use the following to use the value of the functions if they are not null
:
val newName = newName()
val newNumber = newNumber()
val data = Data(
if (newName != null) newName else "",
if (newNumber != null) newNumber else 0
)
But is there a way to just use the default value specified in the constructor of the Data
class when the values are null
?
我在文档中找不到任何东西,但我希望这样的东西能起作用:
val data = Data(newName()?, newNumber()?)
But that does not compile.