Skip to content

Commit

Permalink
Add docstring to ObservableDefaults macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrpb committed Sep 18, 2024
1 parent 21c4281 commit 85587a0
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Sources/DefaultsMacros/ObservableDefaults.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import Defaults
import Foundation

import Defaults
/**
Attached macro that adds support for using ``Defaults`` in ``@Observable`` classes. **Important**: to
prevent issues with ``@Observable``, you'll need to also add ``@ObservationIgnored`` to the attached
property.
This macro adds accessor blocks to the attached property similar to those added by `@Observable`.
For example, given the following source:
```swift
@Observable
final class CatModel {
@ObservableDefaults(Defaults.Keys.cat)
@ObservationIgnored
private var catName: String
}
```
The macro will generate the following expansion:
```swift
@Observable
final class CatModel {
@ObservationIgnored
private var catName: String {
get {
access(keypath: \.catName)
return Defaults[Defaults.Keys.cat]
}
set {
withMutation(keyPath: \catName) {
Defaults[Defaults.Keys.cat] = newValue
}
}
}
}
```
*/
@attached(accessor, names: named(get), named(set))
public macro ObservableDefaults<Value>(_ key: Defaults.Key<Value>) =
#externalMacro(
Expand Down

0 comments on commit 85587a0

Please sign in to comment.