Skip to content

Instantly share code, notes, and snippets.

@jaredh159
Last active April 18, 2024 14:35
Show Gist options
  • Save jaredh159/a3d25712b767db40a3670367097bd84b to your computer and use it in GitHub Desktop.
Save jaredh159/a3d25712b767db40a3670367097bd84b to your computer and use it in GitHub Desktop.
swift @indirect property wrapper
@dynamicMemberLookup
@propertyWrapper
enum Indirect<T> {
indirect case wrapped(T)
var wrappedValue: T {
get { switch self { case .wrapped(let x): return x } }
set { self = .wrapped(newValue) }
}
init(wrappedValue: T) {
self = .wrapped(wrappedValue)
}
subscript<U>(dynamicMember keyPath: KeyPath<T, U>) -> U {
wrappedValue[keyPath: keyPath]
}
}
extension Indirect: Codable where T: Codable {}
extension Indirect: Sendable where T: Sendable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment