来自Kotlin文档:
如果函数没有返回任何有用的值,则其返回类型为Unit.Unit是只有一个值的类型 - Unit.VALUE.不必显式返回此值:
fun printHello(name : String?) : Unit {
if (name != null)
print("Hello, $name!")
else
print("Hi there!")
// We don't need to write 'return Unit.VALUE' or 'return', although we could
}
Run Code Online (Sandbox Code Playgroud)
单位返回功能的目的是什么?为什么有VALUE?什么是VALUE?
kotlin ×1