diff --git a/docs/topics/data-classes.md b/docs/topics/data-classes.md index 8bec9dba2d6..b4f45dd87c0 100644 --- a/docs/topics/data-classes.md +++ b/docs/topics/data-classes.md @@ -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) { @@ -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"} diff --git a/docs/topics/extensions.md b/docs/topics/extensions.md index 6bc322e9201..7d27b46d3ba 100644 --- a/docs/topics/extensions.md +++ b/docs/topics/extensions.md @@ -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() { @@ -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() { @@ -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: