Lambda with Receiver: The DSL Foundation
Understand function types with receivers and how they enable DSL syntax.
Lambda with Receiver: The DSL Foundation is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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")
}Using with to Apply a Receiver Lambda
The standard library function with accepts a receiver and a lambda with receiver. It calls the lambda with the given object as this:
val result = with(StringBuilder()) {
append("Kotlin")
append(" DSLs")
toString()
} // result = "Kotlin DSLs"Declaring Your Own Builder Function
You can write functions that accept lambdas with receivers to create mini-builder APIs:
class HtmlTag(val name: String) {
val children = mutableListOf<HtmlTag>()
fun render(): String = "<$name>${children.joinToString("") { it.render() }}</$name>"
}
fun div(block: HtmlTag.() -> Unit): HtmlTag {
val tag = HtmlTag("div")
tag.block()
return tag
}Calling the Builder
Inside the lambda, this is the HtmlTag, so its members are available directly:
val page = div {
children += HtmlTag("p")
}apply Is a Lambda with Receiver
The scope function apply is the simplest example: it calls a lambda with receiver on this and returns this:
val list = mutableListOf<Int>().apply {
add(1)
add(2)
add(3)
} // list = [1, 2, 3]The DSL Pattern
DSLs are built by nesting builder functions that each accept a lambda with receiver. Each level controls its own receiver, giving a natural, indented structure that mirrors the data hierarchy.
html {
head { title("My Page") }
body {
div {
p("Hello!")
}
}
}Function Types with Receiver
You can store lambdas with receivers in variables using the function type T.() -> R:
val greet: String.() -> String = { "Hello, $this!" }
println("World".greet()) // "Hello, World!"Extension Functions vs. Lambdas with Receiver
Extension functions and lambdas with receiver share the same mechanism: both provide an implicit this. The difference is that an extension function is declared at compile time, while a lambda with receiver is a value that can be passed around.
Composing Receiver Lambdas
Multiple builder functions can be composed to create rich hierarchical DSLs. The key is that each builder function sets its own receiver, keeping scopes isolated.
Type Inference and Receiver Lambdas
Kotlin infers the receiver type from the parameter declaration, so callers do not need to annotate their lambdas. The compiler knows what this is inside the block.
Quick Check
Inside a lambda with receiver of type StringBuilder.() -> Unit, what does this refer to?
Recap: Lambda with Receiver
Key takeaways:
- Lambda with receiver provides an implicit
thisinside the lambda body - Declared as
ReceiverType.() -> ReturnType - Foundation of all Kotlin DSLs
- Standard library uses it in
apply,with,run,buildString, etc.
Frequently asked questions
Is the “Lambda with Receiver: The DSL Foundation” lesson free?
Yes — the full text of “Lambda with Receiver: The DSL Foundation” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Lambda with Receiver: The DSL Foundation”?
Understand function types with receivers and how they enable DSL syntax. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lambda with Receiver: The DSL Foundation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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