Skip to content

Commit 2e0fe5a

Browse files
committed
Alejandro review
1 parent 173fea1 commit 2e0fe5a

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

docs/topics/extensions.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ Kotlin _extensions_ let you extend a class or an interface with new functionalit
44
design patterns like _Decorator_. They are useful when working with third-party libraries you can't modify
55
directly. Once created, you call these extensions as if they were members of the original class or interface.
66

7-
The most common forms of extensions are _extension functions_ and [_extension properties_](#extension-properties), which
8-
are available in [companion objects](#companion-object-extensions) as well as classes and interfaces.
7+
The most common forms of extensions are [_extension functions_](#extension-functions) and [_extension properties_](#extension-properties).
98

10-
Extensions don't modify the classes or interfaces they extend. By defining an extension, you aren't inserting new members,
9+
Importantly, extensions don't modify the classes or interfaces they extend. By defining an extension, you aren't inserting new members,
1110
only making new functions callable or new properties accessible via special syntax.
1211

1312
## Receivers
@@ -156,8 +155,8 @@ For more information about generics, see [generic functions](generics.md).
156155
### Nullable receivers
157156

158157
You can define extension functions with a nullable receiver type, which allows you to call them on a variable
159-
even if its value is null. When the receiver is `null`, `this` is also `null`. To avoid compiler errors, we recommend checking
160-
`this == null` check inside the function body when defining an extension with a nullable receiver.
158+
even if its value is null. When the receiver is `null`, `this` is also `null`. Make sure to handle nullability correctly
159+
within your functions. For example, use `this == null` checks inside function bodies, [safe calls `?.`](null-safety.md#safe-call-operator), or the [Elvis operator `?:`](null-safety.md#elvis-operator).
161160

162161
In this example, you can call the `.toString()` function without checking for `null` because the check already happens inside
163162
the extension function:
@@ -185,7 +184,7 @@ fun main() {
185184
```
186185
{kotlin-runnable="true" kotlin-min-compiler-version="1.3" id="kotlin-extension-function-nullable-receiver"}
187186

188-
### Extension vs member functions
187+
### Extension or member functions?
189188

190189
Since calling extension functions and member functions uses the same notation, how does the compiler know which one to use?
191190
Extension functions are dispatched _statically_, meaning the compiler determines which function to call based on the
@@ -259,7 +258,7 @@ fun main() {
259258
In this example, since an `Int` is passed to the `.printFunctionType()` function, the compiler chooses the extension
260259
function because it matches the signature. The compiler ignores the member function, which takes no arguments.
261260

262-
## Anonymous extension functions
261+
### Anonymous extension functions
263262

264263
You can define extension functions without giving them a name. This is useful when you want to avoid cluttering the global
265264
namespace or when you need to pass some extension behavior as a parameter.
@@ -318,7 +317,7 @@ data class User(val firstName: String, val lastName: String)
318317

319318
// An extension property to get a username-style email handle
320319
val User.emailUsername: String
321-
get() = "${firstName.lowercase()}.${lastName.lowercase()}"
320+
get() = "${firstName.lowercase()}.${lastName.lowercase()}"
322321

323322
fun main() {
324323
val user = User("Mickey", "Mouse")
@@ -343,11 +342,11 @@ data class House(val streetName: String)
343342
// Compiles successfully
344343
val houseNumbers = mutableMapOf<House, Int>()
345344
var House.number: Int
346-
get() = houseNumbers[this] ?: 1
347-
set(value) {
348-
println("Setting house number for ${this.streetName} to $value")
349-
houseNumbers[this] = value
350-
}
345+
get() = houseNumbers[this] ?: 1
346+
set(value) {
347+
println("Setting house number for ${this.streetName} to $value")
348+
houseNumbers[this] = value
349+
}
351350

352351
fun main() {
353352
val house = House("Maple Street")

0 commit comments

Comments
 (0)