Skip to content

Commit

Permalink
chore: clarify paragraphs in Extensions and Data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahhaggarty committed Jul 25, 2023
1 parent 068bd1a commit ae5a5bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 12 additions & 4 deletions docs/topics/data-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ data class Person(val name: String) {
}
```

Only the property `name` will be used inside the `toString()`, `equals()`, `hashCode()`, and `copy()` implementations,
and there will only be one component function `component1()`. While two `Person` objects can have different ages,
they will be treated as equal.
In this example, only the `name` property can be used inside the `toString()`, `equals()`, `hashCode()`, and `copy()` implementations,
and there is only be one component function `component1()`. The `age` property can't be used inside the `toString()`,
`equals()`, `hashCode()`, and `copy()` implementations because it's declared inside the class body. If two `Person`
objects have different ages but the same `name`, then they are treated as equal. This is because the `equals()` function
can only check for equality of the `name` property. For example:

```kotlin
data class Person(val name: String) {
Expand All @@ -67,10 +69,16 @@ fun main() {
val person2 = Person("John")
person1.age = 10
person2.age = 20
//sampleEnd

println("person1 == person2: ${person1 == person2}")
// person1 == person2: true

println("person1 with age ${person1.age}: ${person1}")
// person1 with age 10: Person(name=John)

println("person2 with age ${person2.age}: ${person2}")
// person2 with age 20: Person(name=John)
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
Expand Down
11 changes: 5 additions & 6 deletions docs/topics/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ For more information about generics, see [generic functions](generics.md).
Extensions do not actually modify the classes they extend. By defining an extension, you are not inserting new members into
a class, only making new functions callable with the dot-notation on variables of this type.

Extension functions are dispatched _statically_, which means they are not virtual by receiver type.
An extension function being called is determined by the type of the expression on which the function is invoked,
not by the type of the result from evaluating that expression at runtime. For example:
Extension functions are dispatched _statically_. So which extension function is called is already known at compile time
based on the receiver type. For example:

```kotlin
fun main() {
Expand All @@ -75,8 +74,7 @@ This example prints _Shape_, because the extension function called depends only
parameter `s`, which is the `Shape` class.

If a class has a member function, and an extension function is defined which has the same receiver type,
the same name, and is applicable to given arguments, the _member always wins_.
For example:
the same name, and is applicable to given arguments, the _member always wins_. For example:

```kotlin
fun main() {
Expand Down Expand Up @@ -115,7 +113,8 @@ fun main() {
## Nullable receiver

Note that extensions can be defined with a nullable receiver type. These extensions can be called on an object variable
even if its value is null. If the receiver is `null`, then `this` is also `null`. So when defining an extension with a nullable receiver type, we recommend performing a `this == null` check inside the function body to avoid compiler errors.
even if its value is null. If the receiver is `null`, then `this` is also `null`. So when defining an extension with a
nullable receiver type, we recommend performing a `this == null` check inside the function body to avoid compiler errors.

You can call `toString()` in Kotlin without checking for `null`, as the check already happens inside the extension function:

Expand Down

0 comments on commit ae5a5bd

Please sign in to comment.