Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. @dynamicMemberLookup
  2. struct Apply<Base: AnyObject> {
  3. private let build: () -> Base
  4.  
  5. private init(_ build: @escaping () -> Base) {
  6. self.build = build
  7. }
  8.  
  9. init(_ base: Base) {
  10. self.build = { base }
  11. }
  12.  
  13. subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Base, Value>) -> (Value) -> Apply<Base> {
  14. { [build = build] value in
  15. Apply {
  16. let object = build()
  17. object[keyPath: keyPath] = value
  18. return object
  19. }
  20. }
  21. }
  22.  
  23. func apply() -> Base {
  24. build()
  25. }
  26. }
  27.  
  28. protocol Applicatable {
  29. associatedtype Target: AnyObject
  30. func apply(_ closure: ((Apply<Target>) -> Apply<Target>)) -> Target
  31. }
  32.  
  33. extension Applicatable where Self: AnyObject {
  34. func apply(_ closure: ((Apply<Self>) -> Apply<Self>)) -> Self {
  35. _ = closure(Apply(self)).apply()
  36. return self
  37. }
  38. }
  39.  
  40. extension NSObject: Applicatable {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement