liveBook · Manning

Method invocation

As explained in Kotlin in Action, 2E

The invoke method is a Kotlin function named invoke that enables instances of its containing type to be called with function-call syntax.

Kotlin lets an instance use function-call syntax when its type defines an `operator fun invoke(...)` method. Writing `target(args)` is a concise call to `target.invoke(args)`. This is the same convention behind ordinary calls to lambdas and function-type variables, whose generated `FunctionN` interfaces define `invoke`. Use it when function-like syntax makes an API clearer; avoid it when the call would hide an unclear action. section 13.3.1 section 10.1.3

Make a custom object callable

```kotlin class Greeter(val greeting: String) { operator fun invoke(name: String) { println("$greeting, $name!") } } val greeter = Greeter("Servus") greeter("Dmitry") // calls greeter.invoke("Dmitry") ``` `invoke` may have any parameter list and return type, and a type may overload it. The `operator` modifier enables the parenthesized form; the explicit method call remains available. section 13.3.1

Function calls already use this mechanism. A lambda stored in a function type is an object implementing the matching `FunctionN` interface, so `f(42)` and `f.invoke(42)` execute the same body. The explicit form is particularly useful with nullable function types: `callback?.invoke()` safely calls the callback only when it is non-null. section 10.1.3 section 10.1.4

Use invoke for flexible DSL blocks

An `invoke` method can accept a lambda with receiver, allowing an API to offer both a direct call and a nested block. ```kotlin class DependencyHandler { fun implementation(coordinate: String) { /* add dependency */ } operator fun invoke(body: DependencyHandler.() -> Unit) { body() } } val dependencies = DependencyHandler() dependencies.implementation("group:artifact:version") dependencies { implementation("group:artifact:version") } ``` The second form is `dependencies.invoke({ ... })`. Calling `body()` supplies the handler as the lambda receiver, making `implementation` available without qualification. section 13.3.2

The convention also matters when invoking reflected functions. A specifically typed `KFunctionN` exposes `invoke` with its declared parameter types, so incorrect argument counts or types fail at compile time. By contrast, the generic `KCallable.call` accepts variable arguments and can fail at runtime. section 12.2.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains call-syntax translation, overloads, FunctionN mechanics, nullable calls, reflection, and receiver-lambda DSL implementation.
Examples
Demonstrates a Greeter callable object, nullable transform callback, typed reflected sum function, and dependency-configuration DSL.
What it assumes
Builds from function types and ordinary lambdas, while recalling Kotlin’s named-function convention mechanism.
What you can do after
Build it from scratch by defining operator invoke methods that provide function-like calls or receiver-lambda DSL blocks.
How it connects
Connects function types, lambdas, nullable types, Kotlin reflection, lambdas with receivers, and internal DSLs.

Also explained in

Compare how 2 books explain method invocation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.