Last active
August 29, 2015 14:02
-
-
Save apparentsoft/95707a39f00f444ac9b8 to your computer and use it in GitHub Desktop.
An operator similar to how I'd image ?: to work. Check option and provide a specified (right-hand-side) value if it's nil.
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
| operator infix ||| {associativity left precedence 100} | |
| func ||| <T>(lhs:T?, rhs: @auto_closure () -> T) -> T { | |
| if let alhs = lhs { | |
| return alhs | |
| } else { | |
| return rhs() | |
| } | |
| } | |
| let opt1:Int? = 12 | |
| let a = opt1 ||| 3 | |
| // a is 12 | |
| let opt2:Int? = nil | |
| let opt3:Int? = nil | |
| let b = opt2 ||| opt3 ||| 5 | |
| // b is 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment