-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intermediate tour: Properties chapter #4608
base: intermediate-tour-staging
Are you sure you want to change the base?
Intermediate tour: Properties chapter #4608
Conversation
097b3cd
to
91524c4
Compare
# Conflicts: # docs/kr.tree
91524c4
to
8dffea5
Compare
## Backing fields | ||
|
||
In Kotlin, properties have default `get()` and `set()` functions, known as property accessors, which handle retrieving | ||
and modifying their values. While these default functions are not explicitly visible in the code, they are automatically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While these default functions are not explicitly visible in the code, the compiler automatically generates them to manage
|
||
Just like extension functions, there are also extension properties. Extension properties allow you to add new properties | ||
to existing classes without modifying their source code. However, extension properties in Kotlin do **not** have backing | ||
fields. This means that Kotlin doesn't provide default `get()` and `set()` functions automatically. You have to write them |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it seems like 3 sentences here are about the same idea, maybe shorten it a little bit?
However, extension properties in Kotlin do not have backing fields. This means you need to declare get() and set() functions yourself.
{validate="false"} | ||
|
||
Extension properties are most useful when you want a property to contain a computed value without using inheritance. | ||
You can think of extension properties working like a function that has only a single parameter: the receiver object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can think of extension properties working like a function that has only a single parameter: the receiver object. | |
You can think of extension properties working like a function with only one parameter: the receiver object. |
Extension properties are most useful when you want a property to contain a computed value without using inheritance. | ||
You can think of extension properties working like a function that has only a single parameter: the receiver object. | ||
|
||
For example, let's say that you have a data class called `Person` that has two properties: `firstName`, `lastName`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, let's say that you have a data class called `Person` that has two properties: `firstName`, `lastName`. | |
For example, let's say that you have a data class called `Person` with two properties: `firstName` and `lastName`. |
In these functions: | ||
|
||
* The `operator` keyword marks these functions as operator functions, enabling them to overload the `get()` and `set()` functions. | ||
* The `thisRef` parameter refers to the object **containing** the delegated property. By default, the type is set to `Any?` but you may need to declare a more specific type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* The `thisRef` parameter refers to the object **containing** the delegated property. By default, the type is set to `Any?` but you may need to declare a more specific type. | |
* The `thisRef` parameter refers to the object **containing** the delegated property. By default, the type is set to `Any?`, but you may need to declare a more specific type. |
|
||
<deflist collapsible="true"> | ||
<def title="Hint 2"> | ||
You can use the <a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/build-list.html"><code>buildList()</code></a> function to create and manage a list, instead of manually creating and returning a mutable list. The <code>buildList()</code> function uses a lambda with a receiver, which you learned about in earlier chapters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the <a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/build-list.html"><code>buildList()</code></a> function to create and manage a list, instead of manually creating and returning a mutable list. The <code>buildList()</code> function uses a lambda with a receiver, which you learned about in earlier chapters. | |
You can use the <a href="https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/build-list.html"><code>buildList()</code></a> function to create and manage a list instead of manually creating and returning a mutable list. The <code>buildList()</code> function uses a lambda with a receiver, which you learned about in earlier chapters. |
them whenever it goes below a certain threshold. You have a `Budget` class that is initialized with a `totalBudget` property | ||
that contains the initial budget amount. Within the class, create an observable property called `remainingBudget` that prints: | ||
|
||
* A warning when it is set to lower than 20% of the initial budget. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A warning when it is set to lower than 20% of the initial budget. | |
* A warning when the value is lower than 20% of the initial budget. |
that contains the initial budget amount. Within the class, create an observable property called `remainingBudget` that prints: | ||
|
||
* A warning when it is set to lower than 20% of the initial budget. | ||
* A good news message when the budget is increased from the previous value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A good news message when the budget is increased from the previous value. | |
* An encouraging message when the budget is increased from the previous value. |
* The `get()` function retrieves the property value from the field: `""`. | ||
* The `set()` function accepts `value` as a parameter and assigns it to the field, where `value` is `""`. | ||
|
||
Access to the backing field is useful when you want to perform additional logic in your `get()` or `set()` functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Access to the backing field is useful when you want to perform additional logic in your `get()` or `set()` functions | |
Access to the backing field is useful when you want to add extra logic in your `get()` or `set()` functions |
{style="note"} | ||
|
||
Just like with extension functions, we use extension properties widely in the Kotlin standard library. For example, | ||
consider the [`lastIndex` property](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/last-index.html) for a `CharSequence`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider the [`lastIndex` property](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/last-index.html) for a `CharSequence`. | |
see the [`lastIndex` property](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/last-index.html) for a `CharSequence`. |
This PR is to add the third chapter of the intermediate tour to the staging branch.