Skip to content

Commit 2dfa19c

Browse files
committed
docs: revised 02_DelegatedProperties.md
1 parent 8faefe5 commit 2dfa19c

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

examples/07_Delegation/02_DelegatedProperties.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# Delegated Properties
22

3-
There's some new syntax: you can say `val 'property name': 'Type' by 'expression'`.
4-
The expression after `by` is the delegate, because `get()` and `set()` methods
5-
corresponding to the property will be delegated to it.
6-
Property delegates don't have to implement any interface, but they have
7-
to provide methods named `getValue()` and `setValue()` to be called. The full documentation is [here](http://kotlinlang.org/docs/reference/delegated-properties.html).
8-
9-
Let's try some code.
3+
Kotlin provides a mechanism of [delegated properties](http://kotlinlang.org/docs/reference/delegated-properties.html) that allow delegating the calls of the property `set` and `get` methods to a certain object.
4+
The delegate object in this case should have the method `getValue`. For mutable properties, you'll also need `setValue`.
105

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

@@ -38,12 +33,12 @@ fun main() {
3833

3934
</div>
4035

41-
1. Delegated property `p` of type `String`
42-
2. Delegation methods. For immutable property only `getValue` is required.
36+
1. Delegating property `p` of type `String` to the instance of class `Delegate`. The delegate object is defined after the `by` keyword.
37+
2. Delegation methods. The signatures of these methods are always as shown in the example. Implementations may contain any steps you need. For immutable properties only `getValue` is required.
4338

44-
### Standard delegates
39+
### Standard Delegates
4540

46-
Kotlin standard library contains bunch of useful delegates, like `lazy`, `observable`, etc.
41+
Kotlin standard library contains bunch of useful delegates, like `lazy`, `observable`, etc. You may use them as is.
4742

4843
For example `lazy` is used in case lazy initialization.
4944

@@ -52,33 +47,33 @@ For example `lazy` is used in case lazy initialization.
5247
```kotlin
5348
class LazySample {
5449
init {
55-
println("created!"); // 1
50+
println("created!") // 1
5651
}
5752

58-
val lazy: String by lazy {
53+
val lazyStr: String by lazy {
5954
println("computed!") // 2
6055
"my lazy"
6156
}
6257
}
6358

6459
fun main() {
6560
val sample = LazySample() // 1
66-
println("lazy = ${sample.lazy}") // 2
67-
println("lazy = ${sample.lazy}") // 3
61+
println("lazyStr = ${sample.lazyStr}") // 2
62+
println(" = ${sample.lazyStr}") // 3
6863
}
6964
```
7065

7166
</div>
7267

7368
1. Property `lazy` is not initialized on object creation.
74-
2. The first call to `get()` executes the lambda expression passed to lazy() as an argument and remembers the result
75-
3. Subsequent calls to `get()` simply return the remembered result.
69+
2. The first call to `get()` executes the lambda expression passed to `lazy()` as an argument and saves the result.
70+
3. Further calls to `get()` return the saved result.
7671

77-
If you want thread safety, use blockingLazy() instead: it guarantees that the values will be computed only in one thread, and that all threads will see the same value.
72+
If you want thread safety, use `blockingLazy()` instead: it guarantees that the values will be computed only in one thread and that all threads will see the same value.
7873

79-
### Properties in map
74+
### Storing Properties in a Map
8075

81-
Properties stored in a map. This comes up a lot in applications like parsing JSON
76+
Property delegation can be used for storing properties in a map. This is handy for tasks like parsing JSON
8277
or doing other "dynamic" stuff.
8378

8479
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
@@ -101,6 +96,6 @@ fun main() {
10196

10297
</div>
10398

104-
1. Delegates take values from the `map` (by the string keys - names of properties).
99+
1. Delegates take values from the `map` by the string keys - names of properties.
105100

106-
Of course, you can have mutable property as well, that will modify the map upon assignment (note that you'd need `MutableMap` instead of read-only `Map`).
101+
You can delegate mutable properties to a map as well. In this case, the map will be modified upon property assignments. Note that you will need `MutableMap` instead of read-only `Map`.

0 commit comments

Comments
 (0)