Skip to content

Commit 02181cd

Browse files
committed
Fix: #ifd-out overloaded forms when using Swift 4.2 compiler
The Swift 4.2 compiler doesn't like it when we overload the same function with args that differ only by optional-type (regardless whether we're compiling in Swift 3 mode, Swift 4 mode, or Swift 4.2 mode). * Fixed by wrapping the implicit optional forms of overloads in the `#else` of an `#if (swift(>=3.4) && !swift(>=4.0)) || (swift(>=4.1.50) && !swift(>=4.2)) || swift(>=4.2)`. ▸ Under the Swift 4.2 compiler, Swift 3 mode reports itself as version 3.4, and Swift 4 mode reports itself as version 4.1.50.  This statement checks if we're on Swift ≥ 3.4 (but not ≥ 4), or ≥ 4.1.50 (but not ≥ 4.2), or just ≥ 4.2, and if so excludes the overloads.
1 parent a4e1b0a commit 02181cd

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Sources/NilCoalescingAssignmentOperators.swift

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public func =??<Wrapped>(lhs:inout Wrapped, rhsClosure:(@autoclosure ()throws->W
2929
lhs = rhs!
3030
}
3131
}
32+
3233
/// Assigns only when `rhs` is non-`nil`.
3334
/// - Remark: effectively `lhs = (rhs ?? lhs) ?? nil` _(skipping same-value assignments)_
3435
public func =??<Wrapped>(lhs:inout Wrapped?, rhsClosure:(@autoclosure ()throws->Wrapped?)) rethrows {
@@ -37,14 +38,18 @@ public func =??<Wrapped>(lhs:inout Wrapped?, rhsClosure:(@autoclosure ()throws->
3738
lhs = rhs
3839
}
3940
}
40-
/// Assigns only when `rhs` is non-`nil`.
41-
/// - Remark: effectively `lhs = (rhs ?? lhs) ?? nil` _(skipping same-value assignments)_
42-
public func =??<Wrapped>(lhs:inout Wrapped!, rhsClosure:(@autoclosure ()throws->Wrapped?)) rethrows {
43-
let rhs:Wrapped? = try rhsClosure()
44-
if rhs != nil {
45-
lhs = rhs
41+
42+
#if (swift(>=3.4) && !swift(>=4.0)) || (swift(>=4.1.50) && !swift(>=4.2)) || swift(>=4.2)
43+
#else
44+
/// Assigns only when `rhs` is non-`nil`.
45+
/// - Remark: effectively `lhs = (rhs ?? lhs) ?? nil` _(skipping same-value assignments)_
46+
public func =??<Wrapped>(lhs:inout Wrapped!, rhsClosure:(@autoclosure ()throws->Wrapped?)) rethrows {
47+
let rhs:Wrapped? = try rhsClosure()
48+
if rhs != nil {
49+
lhs = rhs
50+
}
4651
}
47-
}
52+
#endif
4853

4954

5055

@@ -83,13 +88,17 @@ public func ??=<Wrapped>(lhs:inout Wrapped?, rhsClosure:(@autoclosure ()throws->
8388
}
8489
}
8590
}
86-
/// Assigns only when `lhs` is `nil` (and `rhs` is non-`nil`).
87-
/// - Remark: effectively `lhs = (lhs ?? rhs) ?? nil` _(skipping same-value assignments)_
88-
public func ??=<Wrapped>(lhs:inout Wrapped!, rhsClosure:(@autoclosure ()throws->Wrapped?)) rethrows {
89-
if lhs == nil {
90-
let rhs:Wrapped? = try rhsClosure()
91-
if rhs != nil {
92-
lhs = rhs
91+
92+
#if (swift(>=3.4) && !swift(>=4.0)) || (swift(>=4.1.50) && !swift(>=4.2)) || swift(>=4.2)
93+
#else
94+
/// Assigns only when `lhs` is `nil` (and `rhs` is non-`nil`).
95+
/// - Remark: effectively `lhs = (lhs ?? rhs) ?? nil` _(skipping same-value assignments)_
96+
public func ??=<Wrapped>(lhs:inout Wrapped!, rhsClosure:(@autoclosure ()throws->Wrapped?)) rethrows {
97+
if lhs == nil {
98+
let rhs:Wrapped? = try rhsClosure()
99+
if rhs != nil {
100+
lhs = rhs
101+
}
93102
}
94103
}
95-
}
104+
#endif

0 commit comments

Comments
 (0)