How do you read and write a plain Kotlin function type such as (Int, Int) -> String, and what does () -> Unit mean?
answer
- Parentheses = inputs, arrow = output
- () -> Unit is the void callback
- Lambda or ::reference produces the value
- f(x) is sugar for f.invoke(x)
- Unit is Kotlin's void
basics
~20 sIt describes a value that is itself a function. The part in parentheses lists the input types, the arrow points to the result type. () -> Unit means: takes no input and returns nothing useful.
solid answer
~40 sA function type is the type of a value that can be called. The syntax is (ParamTypes) -> ReturnType. The comma-separated types in parentheses are the parameter types (zero or more); the type after -> is the return type. (Int, Int) -> String is a function taking two Ints and producing a String. () -> Unit takes no parameters and returns Unit, Kotlin's equivalent of void, so it is used for side-effecting callbacks like onClick. You assign such a value with a lambda { a, b -> ... } or a function reference (::foo), store it in a val/var, pass it as a parameter, or return it. You call it either with normal call syntax f(1, 2) or explicitly f.invoke(1, 2).
code
kotlin · 6 linesval onClick: () -> Unit = { println("clicked") }
val combine: (Int, Int) -> String = { a, b -> "sum=${a + b}" }
onClick() // prints: clicked
println(combine(2, 3)) // prints: sum=5
println(combine.invoke(4, 5)) // same call, explicit invokego deeper
Can read (A, B) -> C, knows () -> Unit is a no-arg side-effect callback, and can call it with f().
Distinguishes Unit from null/void, knows lambdas and ::references both yield function-type values, and uses invoke explicitly.
Frames function types as first-class values underpinning higher-order functions and callback APIs; mentions Nothing vs Unit nuance.
Connects the notation to API design choices (callback shape, Unit-returning side effects) and to how the compiler desugars it to interfaces.
## What a function type is In Kotlin, functions are first-class values: you can store them in variables, pass them as arguments, and return them. The **type** of such a value is a **function type**. ## The notation The general shape is: ``` (ParameterTypes) -> ReturnType ``` - Inside the parentheses you list the **parameter types**, separated by commas. There can be zero, one, or many. - After the `->` arrow comes the **return type**. Examples: - `() -> Unit` — no parameters, returns `Unit`. `Unit` is Kotlin's analogue of Java's `void`: a type with exactly one value, also written `Unit`. Used for callbacks that only perform a side effect. - `(Int) -> Int` — takes one `Int`, returns an `Int` (e.g. a squaring function). - `(Int, Int) -> String` — takes two `Int`s, returns a `String`. - `(String) -> Boolean` — a predicate. ## Creating values of a function type ```kotlin val log: () -> Unit = { println("tick") } val add: (Int, Int) -> Int = { a, b -> a + b } val len: (String) -> Int = String::length // function reference ``` A **lambda** `{ params -> body }` and a **function reference** `::name` are the two common ways to produce such a value. ## Calling it ```kotlin log() // invoke syntax val s = add(2, 3) // 5 val n = len.invoke("hi") // explicit invoke, same as len("hi") ``` Under the hood `f(args)` is sugar for `f.invoke(args)`. ## Why it matters Function types are the foundation of higher-order functions like `map`, `filter`, `forEach`, and of callback-style APIs. Knowing how to read `(A, B) -> C` lets you understand any such signature at a glance.
- What is the difference between () -> Unit and () -> Nothing?() -> Unit returns normally with the Unit value; () -> Nothing never returns normally (it always throws or loops forever). Nothing is the bottom type with no instances.
- Is (Int) -> Int the same type as a method named square?No. (Int) -> Int is a type; square is a declaration. But you can obtain a value of that type from the method via the reference ::square.
A function type is like a job description: it states what inputs a worker accepts and what they hand back, without naming any specific worker.
saying these in an interview costs you the question
- Reading the arrow backwards (thinking the part after -> is the input)
- Saying () -> Unit returns null instead of the Unit value
- Claiming you must always write .invoke() to call it
- Confusing the function type with a function declaration