Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created February 27, 2016 14:49
Show Gist options
  • Select an option

  • Save ericdke/672ab2441ce463c47d42 to your computer and use it in GitHub Desktop.

Select an option

Save ericdke/672ab2441ce463c47d42 to your computer and use it in GitHub Desktop.
"assign value only if nil" operator like in Ruby
infix operator ||= {}
func ||=<T>(inout left: Optional<T>, right: T) { left = left ?? right }
var foo:String? = nil // foo -> nil
foo ||= "Hello" // foo -> "Hello"
foo ||= "Hi" // foo -> "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment