Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonbs/proxymacro
Swift macro that proxies a value from one object to another.
https://github.com/simonbs/proxymacro
swift swift-macro swift-macros swiftmacro swiftmacros
Last synced: 2 months ago
JSON representation
Swift macro that proxies a value from one object to another.
- Host: GitHub
- URL: https://github.com/simonbs/proxymacro
- Owner: simonbs
- Created: 2023-11-12T22:08:57.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-19T16:08:08.000Z (about 1 year ago)
- Last Synced: 2024-10-10T17:19:28.917Z (2 months ago)
- Topics: swift, swift-macro, swift-macros, swiftmacro, swiftmacros
- Language: Swift
- Homepage:
- Size: 17.6 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ProxyMacro
Swift macro that proxies a property from one object to another.
```swift
final class MyObj {
private final class StateStore {
var number = 42
}@Proxy(\Self.stateStore.number)
var number: Intprivate let stateStore = StateStore()
}
```This is useful when state needs to be kept in sync between multiple objects but we do not want to forward the state using `willSet`/`didSet`. In this case we pass the state store to the children instead. We can still expose the `number` property on `MyObj` and any reads and writes will be forwarded to the `number` propety on `StateStore`.
```swift
final class MyObj {
private final class StateStore {
var number = 42
}@Proxy(\Self.stateStore.number)
var number: Intprivate let stateStore = StateStore()
private lazy var childA = Child(stateStore: stateStore)
private lazy var childB = Child(stateStore: stateStore)
}final class Child {
private let stateStore: StateStoreinit(stateStore: StateStore) {
self.stateStore = stateStore
}
}
```