Skip to content

Instantly share code, notes, and snippets.

@ast3150
Created October 7, 2016 12:29
Show Gist options
  • Select an option

  • Save ast3150/c13a3f47743b4fdf22e1b43aa0581708 to your computer and use it in GitHub Desktop.

Select an option

Save ast3150/c13a3f47743b4fdf22e1b43aa0581708 to your computer and use it in GitHub Desktop.

Revisions

  1. ast3150 created this gist Oct 7, 2016.
    13 changes: 13 additions & 0 deletions AssignOptional.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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
    }
    }