In Kotlin, the function declaration syntax allows you to write equals sign before the curly braces.
Consider these two examples:
=
sign:fun foo1() { println("baz1") }
The code inside the body gets executed by just calling foo1()
.
=
sign:fun foo2() = { println("baz2") }
Here, when foo2()
is called, nothing happens, but to get the body executed one can write foo2()()
.
What is the difference in these two declarations and why do they behave differently?
You can run the code using the following program:
fun main() { foo1() foo2() } /* This code example produces the following results: baz1 */
Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.
To define a function in Kotlin, fun keyword is used. Then comes the name of the function (identifier). Here, the name of the function is callMe . In the above program, the parenthesis ( ) is empty.
In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.
Kotlin functions can be declared at the top level in a file, meaning you do not need to create a class to hold a function, which you are required to do in languages such as Java, C#, and Scala. In addition to top level functions, Kotlin functions can also be declared locally as member functions and extension functions.
Despite visual similarity, the idea of these two declarations is completely different.
Function declaration without equals sign is a Unit
-returning function (similar to Java's void
functions).
What's inside the curly braces is its body, which gets executed right on the function call. The function can be rewritten with Unit
explicitly specified:
fun foo1(): Unit { println("baz1") return Unit }
Kotlin doesn't require the return statement and explicit return type for Unit
-returning functions, and both are usually omitted.
Function declaration with equals sign is a single-expression function, and what it does is just return what's to the right of equals sign.
A simpler example: fun getInt() = 1
is just a shorter form of fun getInt(): Int { return 1 }
.
In foo2
, the right hand side is a lambda expression. The code inside the lambda code block is not executed. In other words, foo2
is a function that returns another function.
Return type of foo2
is () -> Unit
, a function itself, and thus foo2
is a higher-order function.
Without the syntactic sugar and with explicit type, foo2
can be rewritten as
fun foo2(): () -> Unit { val result: () -> Unit = { println("baz2") } return result }
As to the usage, the function which foo2
returns can be stored in a variable, passed around and can later be invoked:
val f = foo2() f() //equivalent to f.invoke()
This is also why foo2()()
in the example executes the code from the lambda body.
Alternatively, we can add ()
at the end when we declare foo2(), as shown in the following example. As such, the lambda expression will be invoked when calling foo3()
. But this is not a good pattern.
fun foo3() = { println("baz3") }()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With