Created
August 23, 2020 19:25
-
-
Save rhysm94/dd0269493987f0b24abdd373c09303c5 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
@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