Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A function literal with receiver is a lambda expression or anonymous function instantiated with an implicit receiver object. It merges the semantics of extension functions with function literals, allowing the body of the literal to access the members of the receiver type directly via the implicit this context, without requiring explicit qualifiers.

Type Declaration

The type of a function literal with receiver is denoted by prefixing the standard function type with the receiver type and a dot:
ReceiverType.(ParameterType) -> ReturnType

Instantiation

You can define a function literal with receiver using either a lambda expression or an anonymous function. Using a Lambda Expression: When using a lambda, the receiver type must be explicitly declared in the variable type, as the compiler cannot infer the receiver type from the lambda body alone.
val appendExclamation: String.() -> String = {
    this + "!" // 'this' refers to the String receiver
}
Using an Anonymous Function: An anonymous function allows you to specify the receiver type directly in the function signature, providing explicit type declaration without relying on the variable’s type.
val appendExclamation = fun String.(): String {
    return this + "!"
}

Invocation Mechanics

A function literal with receiver can be invoked in two ways. The compiler treats the receiver either as an extension target or as the first explicit argument. 1. Extension Function Syntax:
val result1 = "Hello".appendExclamation()
2. Standard Function Syntax:
val result2 = appendExclamation("Hello")

Scope and this Resolution

Inside the body of the function literal, the receiver object becomes the implicit this. All accessible members (properties and functions) of the ReceiverType are available in the lexical scope. The this keyword can be omitted when calling members of the receiver.
val calculate: Int.(Int) -> Int = { multiplier ->
    // 'plus' is called implicitly on the Int receiver
    plus(multiplier) 
}

Type Inference in Higher-Order Functions

When a function literal with receiver is passed as an argument to a higher-order function, the Kotlin compiler infers the receiver type from the higher-order function’s parameter signature. This eliminates the need to explicitly declare the receiver type on the lambda itself.
fun processString(target: String, operation: String.() -> Int): Int {
    // Invoked using extension syntax inside the higher-order function
    return target.operation() 
}

// The compiler infers 'String.() -> Int' for the lambda
val length = processString("Kotlin") {
    length // Implicitly resolves to this.length
}

JVM Representation

Under the hood, the Kotlin compiler translates a function type with a receiver A.(B) -> C into a standard function type where the receiver is passed as the first parameter: (A, B) -> C. The receiver syntax is a compile-time construct designed to manipulate lexical scoping and member resolution, carrying no additional runtime overhead compared to standard lambdas.
Master Kotlin with Deep Grasping Methodology!Learn More