Skip to content

Instantly share code, notes, and snippets.

@hyukhur
Last active May 31, 2017 08:28
Show Gist options
  • Save hyukhur/6af5961a2667ecef32fb7345e978985c to your computer and use it in GitHub Desktop.
Save hyukhur/6af5961a2667ecef32fb7345e978985c to your computer and use it in GitHub Desktop.
Custom Operation and Inline Attribute.
@inline(__always)
func + <K,V>(left: Dictionary<K,V>, right: Dictionary<K,V>) -> Dictionary<K,V> {
return merge(left: left, right: right)
}
func merge<K,V>(left: Dictionary<K,V>, right: Dictionary<K,V>) -> Dictionary<K,V> {
var map = left
for (k, v) in right {
map[k] = v
}
return map
}
extension Dictionary where Value == Any {
func value(forKey key: Key, defaultValue: @autoclosure () -> T) -> T {
guard let value = self[key] as? T else {
return defaultValue()
}
return value
}
}
@hyukhur
Copy link
Author

hyukhur commented Apr 11, 2017

IMHO, The expression of custom operations can use the inline attribute functions. which is the custom operator to easy use and aliased to real function having the contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment