You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/07_Delegation/02_DelegatedProperties.md
+17-22Lines changed: 17 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,7 @@
1
1
# Delegated Properties
2
2
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`.
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.
43
38
44
-
### Standard delegates
39
+
### Standard Delegates
45
40
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.
47
42
48
43
For example `lazy` is used in case lazy initialization.
49
44
@@ -52,33 +47,33 @@ For example `lazy` is used in case lazy initialization.
52
47
```kotlin
53
48
classLazySample {
54
49
init {
55
-
println("created!");// 1
50
+
println("created!") // 1
56
51
}
57
52
58
-
vallazy:String by lazy {
53
+
vallazyStr:String by lazy {
59
54
println("computed!") // 2
60
55
"my lazy"
61
56
}
62
57
}
63
58
64
59
funmain() {
65
60
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
68
63
}
69
64
```
70
65
71
66
</div>
72
67
73
68
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.
76
71
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.
78
73
79
-
### Properties in map
74
+
### Storing Properties in a Map
80
75
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
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.
105
100
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