Created
October 7, 2016 12:29
-
-
Save ast3150/c13a3f47743b4fdf22e1b43aa0581708 to your computer and use it in GitHub Desktop.
Swift: Assign Optional Operator
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
precedencegroup optionalAssignPrecedence { | |
associativity: left | |
higherThan: AssignmentPrecedence | |
} | |
/// If rhs has a value, assigns rhs to lhs | |
/// If rhs is an empty optional, does nothing | |
infix operator ?= : optionalAssignPrecedence | |
func ?=<T: Any>(lhs: inout T, rhs: T?) { | |
if let rhs = rhs { | |
lhs = rhs | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage: