Skip to content

Instantly share code, notes, and snippets.

@Nadohs
Last active December 30, 2015 07:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nadohs/d0103dd401d5fd10ce9c to your computer and use it in GitHub Desktop.
Save Nadohs/d0103dd401d5fd10ce9c to your computer and use it in GitHub Desktop.
if-let operator syntax using flatmap
infix operator ?= {  precedence 90 }

func ?= <T> (inout f: T, optional:T?){
    let _  = optional.flatMap{f = $0}
}

Usage looks like:

store.name ?= json["name"]?.string

Replaces this:

if let name = json["name"]?.string{
    store.name = name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment