Created
February 27, 2016 14:49
-
-
Save ericdke/672ab2441ce463c47d42 to your computer and use it in GitHub Desktop.
"assign value only if nil" operator like in Ruby
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
| infix operator ||= {} | |
| func ||=<T>(inout left: Optional<T>, right: T) { left = left ?? right } | |
| var foo:String? = nil // foo -> nil | |
| foo ||= "Hello" // foo -> "Hello" | |
| foo ||= "Hi" // foo -> "Hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment