Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created March 27, 2015 13:35
Show Gist options
  • Save pyrtsa/c134bdbc3e6153a0a93c to your computer and use it in GitHub Desktop.
Save pyrtsa/c134bdbc3e6153a0a93c to your computer and use it in GitHub Desktop.
An object initialisation operator in Swift
infix operator => { associativity left precedence 100 }
infix operator =>? { associativity left precedence 100 }
/// Perform side effects on `x` and then return it. Example:
///
/// self.label = UILabel(frame: frame) => { l in
/// l.text = "Hello?"
/// }
func =><T>(x: T, f: T -> ()) -> T {
f(x)
return x
}
func =>?<T>(x: T?, f: T -> ()) -> T? {
if let y = x { f(y) }
return x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment