Created
November 14, 2015 15:50
-
-
Save lukaskubanek/d9abe707b4eb8f5d843d to your computer and use it in GitHub Desktop.
OptionalAssignment.swift
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
// Optional Assignment | |
infix operator =? { associativity right precedence 90 } | |
/// Given a variable and an optional value it unwraps the optional value | |
/// and if it is non-nil it assigns the value to the variable. If the value | |
/// is nil it does nothing. | |
public func =? <T>(inout variable: T, optionalValue: T?) { | |
if let value = optionalValue { | |
variable = value | |
} | |
} | |
func before() -> String { | |
var variable: String = "InitialValue" | |
let optionalValue1: String? = nil | |
let optionalValue2: String? = "NewValue" | |
if let value1 = optionalValue1 { | |
variable = value1 // "InitialValue" | |
} | |
if let value2 = optionalValue2 { | |
variable = value2 // "NewValue" | |
} | |
return variable | |
} | |
func after() -> String { | |
var variable: String = "InitialValue" | |
let optionalValue1: String? = nil | |
let optionalValue2: String? = "NewValue" | |
variable =? optionalValue1 // "InitialValue" | |
variable =? optionalValue2 // "NewValue" | |
return variable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment