https://github.com/capnslipp/nilcoalescingassignmentoperators
Combo Operators ??= and =?? (a Swift µ-Library)
https://github.com/capnslipp/nilcoalescingassignmentoperators
null operator swift swift-extensions swift-library
Last synced: about 2 months ago
JSON representation
Combo Operators ??= and =?? (a Swift µ-Library)
- Host: GitHub
- URL: https://github.com/capnslipp/nilcoalescingassignmentoperators
- Owner: capnslipp
- License: unlicense
- Created: 2017-02-13T22:16:30.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-11-16T20:39:37.000Z (over 1 year ago)
- Last Synced: 2026-01-18T21:41:09.172Z (3 months ago)
- Topics: null, operator, swift, swift-extensions, swift-library
- Language: Swift
- Homepage:
- Size: 52.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NilCoalescingAssignmentOperators
NilCoalescingAssignmentOperators is Swift micro-library that provides two nil-coalescing/assignment-combo operators:
## `??=`
**`aVariable ??= newValue`** performs the value assignment if `aVariable` is nil _(like Ruby's `||=` operator)_:
1. If `aVariable` is non-nil, does nothing.
2. If `aVariable` is nil but `newValue` is non-nil, does the assignment: `aVariable = newValue`
3. If `aVariable` & `newValue` are both nil, does nothing.
```swift
aVariable ??= newValue
```
is equivalent to:
```swift
// roughly:
aVariable = aVariable ?? newValue
// precisely:
if aVariable == nil { aVariable = newValue }
```
## `=??`
**`aVariable =?? newValue`** performs the value assignment if `newValue` is non-nil _(like `??=` but prefers the `newValue` over the `aVariable`)_:
1. If `newValue` is nil, does nothing.
2. If `newValue` is non-nil, does the assignment: `aVariable = newValue`
3. If `aVariable` & `newValue` are both non-nil, still does the assignment.
```swift
aVariable =?? newValue
```
is equivalent to:
```swift
// roughly:
aVariable = newValue ?? aVariable
// precisely:
if newValue != nil { aVariable = newValue }
// or
if let newValue { aVariable = newValue }
```
## 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](https://github.com/capnslipp/NilCoalescingAssignmentOperators/tree/swift-4.2), [swift-4](https://github.com/capnslipp/NilCoalescingAssignmentOperators/tree/swift-4), and [swift-3](https://github.com/capnslipp/NilCoalescingAssignmentOperators/tree/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.)_