Last active
July 17, 2020 10:30
-
-
Save grdsdev/dc55c9ac383e4796162df220f49af627 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
| import UIKit | |
| @dynamicMemberLookup | |
| public struct Builder<Base> { | |
| private let _build: () -> Base | |
| public init(_ build: @escaping () -> Base) { | |
| self._build = build | |
| } | |
| public init(_ base: Base) { | |
| self._build = { base } | |
| } | |
| public subscript<Value>(dynamicMember keyPath: WritableKeyPath<Base, Value>) -> (Value) -> Builder<Base> { | |
| return { [build] value in | |
| Builder { | |
| var object = build() | |
| object[keyPath: keyPath] = value | |
| return object | |
| } | |
| } | |
| } | |
| public func build() -> Base { | |
| _build() | |
| } | |
| } | |
| postfix operator .. | |
| @discardableResult | |
| public postfix func .. <T>(object: T) -> Builder<T> { | |
| Builder(object) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example