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-extension-functions.md
+12-17Lines changed: 12 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,26 +19,21 @@ can help you use efficient design patterns to take your projects to the next lev
19
19
20
20
## Extension functions
21
21
22
-
In software development, you often need to modify the behavior of a programwithout altering the original source code.
23
-
For example, in your project, you might want to add extra functionality to a class from a third-party library.
22
+
In software development, you often need to modify a program's behavior without changing the original source code.
23
+
For example, you might want to add extra functionality to a class from a third-party library.
24
24
25
-
Extension functions allow you to extend a class with additional functionality. You call extension functions the same way
26
-
you call member functions of a class.
25
+
You can do this by adding _extension functions_to extend a class. You call extension functions the same way
26
+
you call member functions of a class, using a period `.`.
27
27
28
-
Before introducing the syntax for extension functions, you need to understand the terms **receiver type** and
29
-
**receiver object**.
30
-
31
-
The receiver object is what the function is called on. In other words, the receiver is where or with whom the information is shared.
28
+
Before introducing the complete syntax for extension functions, you need to understand what a **receiver** is.
29
+
The receiver is what the function is called on. In other words, the receiver is where or with whom the information is shared.
32
30
33
31
{width="500"}
34
32
35
-
In this example, the `main()` function calls the [`.first()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/first.html) function.
33
+
In this example, the `main()` function calls the [`.first()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/first.html) function to return the first element in a list.
36
34
The `.first()` function is called **on** the `readOnlyShapes` variable, so the `readOnlyShapes` variable is the receiver.
37
35
38
-
The receiver object has a **type** so that the compiler understands when the function can be used.
39
-
40
-
This example uses the `.first()` function from the standard library to return the first element in a list. To create
41
-
your own extension function, write the name of the class that you want to extend followed by a `.` and the name of
36
+
To create an extension function, write the name of the class that you want to extend followed by a `.` and the name of
42
37
your function. Continue with the rest of the function declaration, including its arguments and return type.
43
38
44
39
For example:
@@ -47,7 +42,7 @@ For example:
47
42
fun String.bold(): String="<b>$this</b>"
48
43
49
44
funmain() {
50
-
// "hello" is the receiver object
45
+
// "hello" is the receiver
51
46
println("hello".bold())
52
47
// <b>hello</b>
53
48
}
@@ -56,11 +51,11 @@ fun main() {
56
51
57
52
In this example:
58
53
59
-
*`String` is the extended class, also known as the receiver type.
54
+
*`String` is the extended class.
60
55
*`bold` is the name of the extension function.
61
56
* The `.bold()` extension function's return type is `String`.
62
-
*`"hello"`, an instance of `String`, is the receiver object.
63
-
* The receiver object is accessed inside the body by the [keyword](keyword-reference.md): `this`.
57
+
*`"hello"`, an instance of `String`, as the receiver.
58
+
* The receiver is accessed inside the body by the [keyword](keyword-reference.md): `this`.
64
59
* A string template (`$`) is used to access the value of `this`.
65
60
* The `.bold()` extension function takes a string and returns it in a `<b>` HTML element for bold text.
* Uses the `with` scope function with the `mainMonitorSecondaryBufferBackedCanvas` instance as the receiver object.
395
+
* Uses the `with` scope function with the `mainMonitorSecondaryBufferBackedCanvas` instance as the receiver.
396
396
* Creates a temporary scope within the `with` scope function so that you don't have to explicitly refer to the `mainMonitorSecondaryBufferBackedCanvas` instance when calling its member functions.
397
397
* Passes a lambda expression to the `with` scope function that calls a sequence of member functions with different function parameters.
0 commit comments