Lambda with Receiver: The DSL Foundation
Understand function types with receivers and how they enable DSL syntax.
What Is a Lambda with Receiver?
A lambda with receiver is a function literal where the body has access to the members of a designated receiver object — without any qualifier. The type looks like: (ReceiverType) -> ReturnType, written as ReceiverType.() -> ReturnType.
Syntax Comparison
A regular lambda: { value: Int -> value + 1 }. A lambda with receiver: StringBuilder.() -> Unit — inside this lambda, this refers to the StringBuilder instance.
val addHello: StringBuilder.() -> Unit = {
append("Hello") // this = StringBuilder
append(", World")
}All lessons in this course
- Lambda with Receiver: The DSL Foundation
- @DslMarker: Preventing Receiver Leakage
- Building a Type-Safe HTML/Config DSL
- Testing and Evolving DSLs Without Breaking Users