Last active
August 25, 2022 17:02
-
-
Save jayrhynas/7d748f690acd640d7eae65a11bae052b to your computer and use it in GitHub Desktop.
This file contains 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
@dynamicMemberLookup | |
struct Builder<Object> { | |
let object: Object | |
init(_ object: Object) { | |
self.object = object | |
} | |
subscript<T>(dynamicMember keyPath: WritableKeyPath<Object, T>) -> (T) -> Builder<Object> { | |
return { | |
var copy = object | |
copy[keyPath: keyPath] = $0 | |
return Builder(copy) | |
} | |
} | |
func call(_ work: (Object) -> Void) -> Builder<Object> { | |
work(object) | |
return self | |
} | |
} |
This file contains 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
// configure an existing object | |
let view1 = UIView() | |
Builder(view1) | |
.backgroundColor(.white) | |
.call { $0.addSubview(UIView()) } | |
.isExclusiveTouch(true) | |
// create a new object | |
let view2 = Builder(UIView()) | |
.backgroundColor(.black) | |
.object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
Setter
type is required because we can't add@discardableResult
to closures