Combo Operators ??= and =?? (a Swift ยต-Library)

What's New




Full Changelog: 0.3.2...0.3.3


NilCoalescingAssignmentOperators is Swift micro-library that provides two nil-coalescing/assignment-combo operators:


lhs ??= rhs works like Ruby's ||= operator:

  1. If lhs is non-nil, does nothing.
  2. If lhs is nil but rhs is non-nil, does the assignment: lhs = rhs
  3. If lhs & rhs are both nil, does nothing.
lhs ??= rhs

is equivalent to:

// roughly:
lhs = lhs ?? rhs

// precisely:
if lhs == nil { lhs = rhs }


lhs =?? rhs works similarly, but prefers the rhs over the lhs:

  1. If rhs is nil, does nothing.
  2. If rhs is non-nil, does the assignment: lhs = rhs
  • If lhs & rhs are both non-nil, still does the assignment.
lhs =?? rhs

is equivalent to:

// roughly:
lhs = rhs ?? lhs

// precisely:
if rhs != nil { lhs = rhs }
// or
if let rhs = rhs { lhs = rhs }

Build Overlays

The master branch is Swift 5.x, and build overlays (the minimal changeset to the Package.swift, xcodeproj, and other build files) of the current library version are available on the swift-4.2, swift-4, and swift-3 branches. (Note: I don't check that these are built as often as I used to when Swift 4.2 or 4.0 were the latest versions, but their changes haved worked and I've merged new library versions into them since then.)


  • Swift Tools 5.0.0
View More Packages from this Author


  • None
Last updated: Sun Mar 12 2023 21:01:47 GMT-0500 (GMT-05:00)