Skip to content

Instantly share code, notes, and snippets.

@rhysm94
Created August 23, 2020 19:25
Show Gist options
  • Save rhysm94/dd0269493987f0b24abdd373c09303c5 to your computer and use it in GitHub Desktop.
Save rhysm94/dd0269493987f0b24abdd373c09303c5 to your computer and use it in GitHub Desktop.
@dynamicMemberLookup
class Observable<T> { }
extension Observable {
func map<Result>(_ transform: @escaping (T) -> Result) -> Observable<Result> {
// Apply transform function here
fatalError()
}
subscript<Property>(dynamicMember keyPath: KeyPath<T, Property>) -> Observable<Property> {
map { $0[keyPath: keyPath] }
}
}
struct ShoppingList {
var items: [String] = []
}
let shoppingList = Observable<ShoppingList>()
// With Map
let mapItems = shoppingList.map { $0.items }
// With dynamicMemberLookup
let lookupItems = shoppingList.items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment