Created
November 5, 2014 11:13
-
-
Save nubbel/cde92a45a133228efb2e 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
infix operator ??= { | |
associativity right | |
precedence 90 | |
assignment | |
} | |
func ??=<T>(inout optional: T?, defaultValue: @autoclosure () -> T?) -> T? { | |
optional = optional ?? defaultValue() | |
return optional | |
} | |
var x: Int? = 2 | |
var y: Int? = nil | |
x ??= 99 | |
x // => 2 | |
y ??= 99 | |
y // 99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, I'm a noob in Swift and this is really nice to have :-)