+ Ma is now Delegate
+ way to add pure Lyng parents when declaring kotlin bindings
This commit is contained in:
1 parent
3ef68d8bb4
commit
3941ddee40
7 files changed
+136
-2
No files matched your search
+13
@@ -224,6 +224,19 @@ A delegate is any object that provides the following methods (all optional depen
|
||||
- `invoke(thisRef, name, args...)`: Called when a delegated `fun` is invoked.
|
||||
- `bind(name, access, thisRef)`: Called once during initialization to configure or validate the delegate.
|
||||
|
||||
### Map as a Delegate
|
||||
|
||||
Maps can also be used as delegates. When delegated to a property, the map uses the property name as the key:
|
||||
|
||||
```lyng
|
||||
val settings = { "theme": "dark", "fontSize": 14 }
|
||||
val theme by settings
|
||||
var fontSize by settings
|
||||
|
||||
println(theme) // "dark"
|
||||
fontSize = 16 // Updates settings["fontSize"]
|
||||
```
|
||||
|
||||
For more details and advanced patterns (like `lazy`, `observable`, and shared stateless delegates), see the [Delegation Guide](delegation.md).
|
||||
|
||||
## Instance initialization: init block
|
||||
|
||||
@@ -151,6 +151,24 @@ fun test() {
|
||||
}
|
||||
```
|
||||
|
||||
### 6. Map as a Delegate
|
||||
|
||||
Maps can be used as delegates for `val` and `var` properties. When a map is used as a delegate, it uses the property name as a key to read from or write to the map.
|
||||
|
||||
```lyng
|
||||
val m = { "a": 1, "b": 2 }
|
||||
val a by m
|
||||
var b by m
|
||||
|
||||
println(a) // 1
|
||||
println(b) // 2
|
||||
|
||||
b = 42
|
||||
println(m["b"]) // 42
|
||||
```
|
||||
|
||||
Because `Map` implements `getValue` and `setValue`, it works seamlessly with any object that needs to store its properties in a map (e.g., when implementing dynamic schemas or JSON-backed objects).
|
||||
|
||||
## The `bind` Hook
|
||||
|
||||
The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to:
|
||||
|
||||
Reference in new issue
Block a user