Prefer usage of `Unit` over `Void`KT-W1071
Unit is to Kotlin what void is in Java, in the same way
as Any is used in place of
Object. Thus, unless interoperability with Java based
code, especially with generics is required, you should always use the Unit type. The
Void type has no real use case in Kotlin other than when
used with generic type arguments to Java types. This is becauseUnit itself acts as the "void" type in Kotlin. When a
function returns Unit, Java sees only the primitive void type.
Unit is also not just a type, but the sole value of that type (Technically, Unit is declared as an object. One important
point to note is that in Kotlin, every function must return a value (unless the return type is
Nothing), even if there is no value to return.
The way Kotlin accomplishes this is by returning the sole existing instance of Unit from any function with no return value.
Meanwhile, Void has no instances and its constructor is private, meaning it cannot be instantiated, and thus cannot be
used in Kotlin.
Bad Practice
fun myFun(): Void {
// ..
}
Recommended
Replace usages of Void with Unit in function and variable declarations.
fun myFun(): Unit {
// ..
}
// ...or better
fun myFun() {
// ..
}