Created
March 24, 2016 20:17
-
-
Save gregomni/3e6b3541e5c8428335ee 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
struct DefaultedDictionary<Key: Hashable, Value> { | |
var dict: [Key:Value] | |
let fallback: Value | |
subscript(key: Key) -> Value { | |
get { | |
return dict[key] ?? fallback | |
} | |
set { | |
dict[key] = newValue | |
} | |
} | |
} | |
extension Dictionary { | |
func defaultingTo(value: Value) -> DefaultedDictionary<Key, Value> { | |
return DefaultedDictionary(dict: self, fallback: value) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment