Created
November 12, 2019 07:30
-
-
Save giginet/ec00a2420cdf2cd3aef1f0a18a9cf00d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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