You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/tour/kotlin-tour-intermediate-lambdas-receiver.md
+33-20Lines changed: 33 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,50 +20,64 @@ can help you create a domain-specific language.
20
20
## Lambda expressions with receiver
21
21
22
22
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.
25
25
26
26
> Lambda expressions with receiver are also known as function literals with receiver.
27
27
>
28
28
{style="tip"}
29
29
30
30
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:
32
32
33
33
```kotlin
34
34
MutableList<Int>.() ->Unit
35
35
```
36
36
37
37
This function type has:
38
38
39
-
*`MutableList<Int>` as the receiver type.
39
+
*`MutableList<Int>` as the receiver.
40
40
* No function parameters within the parentheses `()`.
41
41
* No return value: `Unit`.
42
42
43
-
Consider this example that extends the `StringBuilder` class:
43
+
Consider this example that draws shapes on a canvas:
44
44
45
45
```kotlin
46
-
funmain() {
47
-
// Lambda expression with receiver definition
48
-
fun StringBuilder.appendText() { append("Hello!") }
* 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.
67
81
68
82
Lambda expressions with receiver are helpful when you want to create a domain-specific language (DSL). Since you have
69
83
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) {
85
99
```
86
100
87
101
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:
90
103
91
104
```kotlin
92
105
funmenu(name:String, init: Menu.() ->Unit): Menu {
0 commit comments