Kotlin function declaration: equals sign before curly braces
Asked Answered
M

1

46

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.
Meurer answered 16/3, 2016 at 13:43 Comment(2)
Kotlin or the IDE should probably have an inspection for that. youtrack.jetbrains.com/issue/KT-11461Gunpaper
My problem was about braces appearing after a function call. This medium article explains the varying syntactical alternatives.Mentalist
M
73

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")
}()
Meurer answered 16/3, 2016 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.