Skip to content

Commit 9e4d8b7

Browse files
p7novAlexanderPrendota
authored andcommitted
feat: add scope functions (Kotlin#70)
* docs: added scope functions, renamed chapter stdlib * docs: added scope functions, renamed chapter stdlib * docs: text corrections after proofread
1 parent 008cba5 commit 9e4d8b7

File tree

8 files changed

+143
-69
lines changed

8 files changed

+143
-69
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Scope Functions
2+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# let
2+
3+
The Kotlin standard library function `let` can be used for scoping and null-checks. When called on an object, `let` executes the given block of code and returns the result of its last expression.
4+
The object is accessible inside the block by the reference `it`.
5+
6+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
7+
8+
```kotlin
9+
fun customPrint(s: String) {
10+
print(s.toUpperCase())
11+
}
12+
13+
fun main() {
14+
//sampleStart
15+
val empty = "testKotlin".let { // 1
16+
customPrint(it) // 2
17+
it.isEmpty() // 3
18+
}
19+
println(" is empty: $empty")
20+
21+
22+
fun printNonNull(str: String?) {
23+
println("Printing \"$str\":")
24+
25+
str?.let { // 4
26+
print("\t")
27+
customPrint(it)
28+
println()
29+
}
30+
}
31+
printNonNull(null)
32+
printNonNull("my string")
33+
//sampleEnd
34+
}
35+
```
36+
37+
</div>
38+
39+
1. Calls the given block on the result on the string "_test_".
40+
2. Calls the function on "_test_" by the `it` reference.
41+
3. `let` returns the value of this expression.
42+
4. Uses safe call, so `let` and its code block will be executed only on non-null values.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# run
2+
3+
Like [`let`](01_let.md), `run` is another scoping function from the standard library. Basically, it does the same: executes a code block and returns its result.
4+
The difference is that inside `run` the object is accessed by `this`. This is useful when you want to call the object's methods rather than pass it as an argument.<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
5+
6+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
7+
8+
```kotlin
9+
10+
fun main() {
11+
//sampleStart
12+
fun getNullableLength(ns: String?) {
13+
println("for \"$ns\":")
14+
ns?.run { // 1
15+
println("\tis empty? " + isEmpty()) // 2
16+
println("\tlength = $length")
17+
length // 3
18+
}
19+
}
20+
getNullableLength(null)
21+
getNullableLength("")
22+
getNullableLength("some string with Kotlin")
23+
//sampleEnd
24+
}
25+
```
26+
27+
</div>
28+
29+
30+
1. Calls the given block on a nullable variable.
31+
2. Inside `run`, the object's members are accessed without its name.
32+
3. `run` returns the `length` of the given `String` if it's not `null`.

examples/06_stdlib/02_withFunction.md renamed to examples/06_scope_functions/03_with.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
2+
13
# with
24

3-
*with* function lets you access members of an object in a short and concise way. Inside the `with` block you can refer to object members without specifying its name as a prefix.
5+
`with` is a non-extension function that can access members of its argument concisely: you can omit the instance name when referring to its members.
46

57
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
68

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# apply
2+
3+
`apply` executes a block of code on an object and returns the object itself. Inside the block, the object is referenced by `this`.
4+
This function is handy for initializing objects.
5+
6+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
7+
8+
```kotlin
9+
data class Person(var name: String, var age: Int, var about: String) {
10+
constructor() : this("", 0, "")
11+
}
12+
13+
fun main() {
14+
//sampleStart
15+
val jake = Person() // 1
16+
val stringDescription = jake.apply { // 2
17+
name = "Jake" // 3
18+
age = 30
19+
about = "Android developer"
20+
}.toString() // 4
21+
//sampleEnd
22+
println(stringDescription)
23+
}
24+
```
25+
26+
</div>
27+
28+
29+
1. Creates a `Person()` instance with default property values.
30+
2. Applies the code block (next 3 lines) to the instance.
31+
3. Inside `apply`, it's equivalent to `jake.name = "Jake"`.
32+
4. The return value is the instance itself, so you can chain other operations.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# also
2+
3+
`also` works like [`apply`](04_apply.md): it executes a given block and returns the object called.
4+
Inside the block, the object is referenced by `it`, so it's easier to pass it as an argument.
5+
This function is handy for embedding additional actions, such as logging in call chains.
6+
7+
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
8+
9+
```kotlin
10+
data class Person(var name: String, var age: Int, var about: String) {
11+
constructor() : this("", 0, "")
12+
}
13+
14+
fun writeCreationLog(p: Person) {
15+
println("A new person ${p.name} was created.")
16+
}
17+
18+
fun main() {
19+
//sampleStart
20+
val jake = Person("Jake", 30, "Android developer") // 1
21+
.also { // 2
22+
writeCreationLog(it) // 3
23+
}
24+
//sampleEnd
25+
}
26+
```
27+
28+
</div>
29+
30+
1. Creates a `Person()` object with the given property values.
31+
2. Applies the given code block to the object. The return value is the object itself.
32+
3. Calls the logging function passing the object as an argument.

examples/06_stdlib/00_description.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/06_stdlib/01_letFunction.md

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)