Skip to content
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

Open
wants to merge 1 commit into
base: intermediate-tour-staging
Choose a base branch
from

Conversation

sarahhaggarty
Copy link
Collaborator

This PR is to add the third chapter of the intermediate tour to the staging branch.

@sarahhaggarty sarahhaggarty requested a review from a team as a code owner December 11, 2024 11:08
@sarahhaggarty sarahhaggarty force-pushed the intermediate-tour-properties branch from 097b3cd to 91524c4 Compare December 11, 2024 11:09
@danil-pavlov danil-pavlov self-assigned this Dec 11, 2024
# Conflicts:
#	docs/kr.tree
@sarahhaggarty sarahhaggarty force-pushed the intermediate-tour-properties branch from 91524c4 to 8dffea5 Compare December 11, 2024 16:00
## 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
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants