Unit-returning Method Inspection


#1
fun work1(): Unit {
  work2()
}

When I create a function by this way, IDEA shows an inspection that the method explicitly returns Unit.

fun work1(): Unit = work2()

But for this syntax there’s no warnings.

Why??


#2

Because in kotlin you should always (at least for public apis) declare the return type explicitly when using the second syntax. That way the return type of work1 will not be changed automatically when you change the return type of work2.
This is important when working on libraries, because you don’t normally want to change your public api.
When you use the normal function syntax, the return type does not depend on the expression, therefor you don’t need to specify the return type as Unit.


#3

I’ve understood, thanks