Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin function declaration: equals sign before curly braces

In Kotlin, the function declaration syntax allows you to write equals sign before the curly braces.

Consider these two examples:

  1. Without = sign:
fun foo1() {     println("baz1") } 

The code inside the body gets executed by just calling foo1().

  1. With = 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 */ 

This question, though having not much meaning, is [intentionally asked and answered by the author][1], because a few questions have already been posted where people got problems because of incorrect function definitions.
like image 593
hotkey Avatar asked Mar 16 '16 13:03

hotkey


People also ask

What is () -> unit in Kotlin?

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.

How do I declare a function in Kotlin?

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.

What is Vararg in Kotlin?

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.

Which is a valid function declaration in Kotlin?

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.


1 Answers

Despite visual similarity, the idea of these two declarations is completely different.

1. Function declaration without equals sign

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.

2. Function declaration with equals sign

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") }() 
like image 185
hotkey Avatar answered Oct 20 '22 13:10

hotkey