Skip to content

Commit f1fd422

Browse files
committed
chore: simplify receiver description in intermediate tour
1 parent b964858 commit f1fd422

5 files changed

+17
-22
lines changed
-2.77 KB
Loading

docs/topics/tour/kotlin-tour-intermediate-extension-functions.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@ can help you use efficient design patterns to take your projects to the next lev
1919

2020
## Extension functions
2121

22-
In software development, you often need to modify the behavior of a program without 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.
2424

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 `.`.
2727

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.
3230

3331
![An example of sender and receiver](receiver-highlight.png){width="500"}
3432

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.
3634
The `.first()` function is called **on** the `readOnlyShapes` variable, so the `readOnlyShapes` variable is the receiver.
3735

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
4237
your function. Continue with the rest of the function declaration, including its arguments and return type.
4338

4439
For example:
@@ -47,7 +42,7 @@ For example:
4742
fun String.bold(): String = "<b>$this</b>"
4843

4944
fun main() {
50-
// "hello" is the receiver object
45+
// "hello" is the receiver
5146
println("hello".bold())
5247
// <b>hello</b>
5348
}
@@ -56,11 +51,11 @@ fun main() {
5651

5752
In this example:
5853

59-
* `String` is the extended class, also known as the receiver type.
54+
* `String` is the extended class.
6055
* `bold` is the name of the extension function.
6156
* 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`.
6459
* A string template (`$`) is used to access the value of `this`.
6560
* The `.bold()` extension function takes a string and returns it in a `<b>` HTML element for bold text.
6661

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<img src="icon-9-todo.svg" width="20" alt="Ninth step" /> <a href="kotlin-tour-intermediate-libraries-and-apis.md">Libraries and APIs</a></p>
1515
</tldr>
1616

17-
In this chapter, you'll learn how to use receiver objects with another type of function, lambda expressions, and how they
17+
In this chapter, you'll learn how to use receivers with another type of function, lambda expressions, and how they
1818
can help you create a domain-specific language.
1919

2020
## Lambda expressions with receiver
@@ -80,7 +80,7 @@ In this example:
8080
directly as if they are inside the `Canvas` class.
8181

8282
Lambda expressions with receiver are helpful when you want to create a domain-specific language (DSL). Since you have
83-
access to the receiver object's member functions and properties without explicitly referencing the receiver, your code
83+
access to the receiver's member functions and properties without explicitly referencing the receiver, your code
8484
becomes leaner.
8585

8686
To demonstrate this, consider an example that configures items in a menu. Let's begin with a `MenuItem` class and a

docs/topics/tour/kotlin-tour-intermediate-properties.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fields. This means that you need to write the `get()` and `set()` functions your
126126
field means that they can't hold any state.
127127

128128
To declare an extension property, write the name of the class that you want to extend followed by a `.` and the name of
129-
your property. Just like with normal class properties, you need to declare a receiver type for your property.
129+
your property. Just like with normal class properties, you need to declare a type for your property.
130130
For example:
131131

132132
```kotlin
@@ -135,7 +135,7 @@ val String.lastChar: Char
135135
{validate="false"}
136136

137137
Extension properties are most useful when you want a property to contain a computed value without using inheritance.
138-
You can think of extension properties working like a function with only one parameter: the receiver object.
138+
You can think of extension properties working like a function with only one parameter: the receiver.
139139

140140
For example, let's say that you have a data class called `Person` with two properties: `firstName` and `lastName`.
141141

docs/topics/tour/kotlin-tour-intermediate-scope-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fun main() {
392392
{kotlin-runnable="true" id="kotlin-tour-scope-function-with-after"}
393393

394394
This example:
395-
* 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.
396396
* 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.
397397
* Passes a lambda expression to the `with` scope function that calls a sequence of member functions with different function parameters.
398398

0 commit comments

Comments
 (0)