Skip to content

Commit f27722d

Browse files
committed
update: replace StringBuilder example in intermediate tour
1 parent 1a89f7a commit f27722d

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

docs/topics/tour/kotlin-tour-intermediate-lambdas-receiver.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,64 @@ can help you create a domain-specific language.
2020
## Lambda expressions with receiver
2121

2222
In the beginner tour, you learned how to use [lambda expressions](kotlin-tour-functions.md#lambda-expressions). Lambda expressions can also have a receiver.
23-
In this case, lambda expressions can access any member functions or properties of the receiver object without having
24-
to explicitly specify the receiver object each time. Without these additional references, your code is easier to read and maintain.
23+
In this case, lambda expressions can access any member functions or properties of the receiver without having
24+
to explicitly specify the receiver each time. Without these additional references, your code is easier to read and maintain.
2525

2626
> Lambda expressions with receiver are also known as function literals with receiver.
2727
>
2828
{style="tip"}
2929

3030
The syntax for a lambda expression with receiver is different when you define the function type. First, write the receiver
31-
type that you want to extend. Next, put a `.` and then complete the rest of your function type definition. For example:
31+
that you want to extend. Next, put a `.` and then complete the rest of your function type definition. For example:
3232

3333
```kotlin
3434
MutableList<Int>.() -> Unit
3535
```
3636

3737
This function type has:
3838

39-
* `MutableList<Int>` as the receiver type.
39+
* `MutableList<Int>` as the receiver.
4040
* No function parameters within the parentheses `()`.
4141
* No return value: `Unit`.
4242

43-
Consider this example that extends the `StringBuilder` class:
43+
Consider this example that draws shapes on a canvas:
4444

4545
```kotlin
46-
fun main() {
47-
// Lambda expression with receiver definition
48-
fun StringBuilder.appendText() { append("Hello!") }
46+
class Canvas {
47+
fun drawCircle() = println("🟠 Drawing a circle")
48+
fun drawSquare() = println("🟥 Drawing a square")
49+
}
4950

51+
// Lambda expression with receiver definition
52+
fun render(block: Canvas.() -> Unit): Canvas {
53+
val canvas = Canvas()
5054
// Use the lambda expression with receiver
51-
val stringBuilder = StringBuilder()
52-
stringBuilder.appendText()
53-
println(stringBuilder.toString())
54-
// Hello!
55+
canvas.block()
56+
return canvas
57+
}
58+
59+
fun main() {
60+
render {
61+
drawCircle()
62+
// 🟠 Drawing a circle
63+
drawSquare()
64+
// 🟥 Drawing a square
65+
}
5566
}
5667
```
5768
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="kotlin-intermediate-tour-lambda-expression-with-receiver"}
5869

5970
In this example:
6071

61-
* The `StringBuilder` class is the receiver type.
62-
* The function type of the lambda expression has no function parameters `()` and has no return value `Unit`.
63-
* The lambda expression calls the `append()` member function from the `StringBuilder` class and uses the string `"Hello!"` as the function parameter.
64-
* An instance of the `StringBuilder` class is created.
65-
* The lambda expression assigned to `appendText` is called on the `stringBuilder` instance.
66-
* The `stringBuilder` instance is converted to string with the `toString()` function and printed via the `println()` function.
72+
* The `Canvas` class has two functions that simulate drawing a circle or a square.
73+
* The `render()` function takes a `block` parameter and returns an instance of the `Canvas` class.
74+
* The `block` parameter is a lambda expression with receiver, where the `Canvas` class is the receiver.
75+
* The `render()` function creates an instance of the `Canvas` class and calls the `block()` lambda expression on the `canvas` instance, using it as the receiver.
76+
* The `main()` function calls the `render()` function with a lambda expression, which is passed to the `block` parameter.
77+
* Inside the lambda passed to the `render()` function, the program calls the `drawCircle()` and `drawSquare()` functions on an instance of the `Canvas` class.
78+
79+
Because the `drawCircle()` and `drawSquare()` functions are called in the lambda expression with receiver, they can be called
80+
directly as if they are inside the `Canvas` class.
6781

6882
Lambda expressions with receiver are helpful when you want to create a domain-specific language (DSL). Since you have
6983
access to the receiver object's member functions and properties without explicitly referencing the receiver, your code
@@ -85,8 +99,7 @@ class Menu(val name: String) {
8599
```
86100

87101
Let's use a lambda expression with receiver passed as a function parameter (`init`) to the `menu()` function that builds
88-
a menu as a starting point. You'll notice that the code follows a similar approach to the previous example with the
89-
`StringBuilder` class:
102+
a menu as a starting point:
90103

91104
```kotlin
92105
fun menu(name: String, init: Menu.() -> Unit): Menu {

0 commit comments

Comments
 (0)