Skip to content

Instantly share code, notes, and snippets.

@giginet
Created November 12, 2019 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giginet/ec00a2420cdf2cd3aef1f0a18a9cf00d to your computer and use it in GitHub Desktop.
Save giginet/ec00a2420cdf2cd3aef1f0a18a9cf00d to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
@propertyWrapper struct Delegate<T, Root> {
private var keyPath: ReferenceWritableKeyPath<Root, T>
private var delegate: Root
init(wrappedValue: T, _ keyPath: ReferenceWritableKeyPath<Root, T>, to delegate: Root) {
self.keyPath = keyPath
self.delegate = delegate
}
var wrappedValue: T {
get { delegate[keyPath: keyPath] }
set { delegate[keyPath: keyPath] = newValue }
}
}
class Inner {
var a: Int?
init() { }
}
struct Foo {
private var inner: Inner = Inner()
@Delegate(\Inner.a, to: inner) lazy var a: Int? = nil // impossible 😢
}
var f = Foo()
print(f.a)
f.a = 10
print(f.a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment