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
Kotlin supports easy implementation of the [delegation pattern](https://kotlinlang.org/docs/reference/delegation.html) on the native level without any boilerplate code.
Copy file name to clipboardExpand all lines: examples/07_Delegation/02_DelegatedProperties.md
+18-24Lines changed: 18 additions & 24 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 allows 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.
43
-
44
-
### Standard delegates
36
+
1. Delegates 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.
45
38
46
-
Kotlin standard library contains bunch of useful delegates, like `lazy`, `observable`, etc.
39
+
### Standard Delegates
47
40
48
-
For example `lazy` is used in case lazy initialization.
41
+
The Kotlin standard library contains a bunch of useful delegates, like `lazy`, `observable`, and other. You may use them as is.
42
+
For example `lazy`is used for lazy initialization.
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.
68
+
2. The first call to `get()` executes the lambda expression passed to `lazy()` as an argument and saves the result.
69
+
3.Further calls to `get()` return the saved result.
76
70
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.
71
+
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
72
79
-
### Properties in map
73
+
### Storing Properties in a Map
80
74
81
-
Properties stored in a map. This comes up a lot in applications like parsing JSON
75
+
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).
98
+
1. Delegates take values from the `map` by the string keys - names of properties.
105
99
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`).
100
+
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