NilCoalescingAssignmentOperators

master

A Swift ยต-Library for ??/= Combo Operators ??= and =??
capnslipp/NilCoalescingAssignmentOperators

NilCoalescingAssignmentOperators

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 let rhs = rhs { lhs = rhs }
// or
if rhs != nil { lhs = rhs }

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Sep 18 2022 19:08:06 GMT-0500 (GMT-05:00)