Last active
May 31, 2017 08:28
-
-
Save hyukhur/6af5961a2667ecef32fb7345e978985c to your computer and use it in GitHub Desktop.
Custom Operation and Inline Attribute.
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
@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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.