Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @dynamicMemberLookup
- struct Apply<Base: AnyObject> {
- private let build: () -> Base
- private init(_ build: @escaping () -> Base) {
- self.build = build
- }
- init(_ base: Base) {
- self.build = { base }
- }
- subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Base, Value>) -> (Value) -> Apply<Base> {
- { [build = build] value in
- Apply {
- let object = build()
- object[keyPath: keyPath] = value
- return object
- }
- }
- }
- func apply() -> Base {
- build()
- }
- }
- protocol Applicatable {
- associatedtype Target: AnyObject
- func apply(_ closure: ((Apply<Target>) -> Apply<Target>)) -> Target
- }
- extension Applicatable where Self: AnyObject {
- func apply(_ closure: ((Apply<Self>) -> Apply<Self>)) -> Self {
- _ = closure(Apply(self)).apply()
- return self
- }
- }
- extension NSObject: Applicatable {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement