> ## 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.

# Kotlin Function Literal with Receiver

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:

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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:**

```kotlin theme={"dark"}
val result1 = "Hello".appendExclamation()
```

**2. Standard Function Syntax:**

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
