Skip to content

Commit 71aa4a3

Browse files
committed
[OrderedDictionary] Resolve spurious(?) call-site ambiguities
1 parent fe94c36 commit 71aa4a3

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Initializers.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extension OrderedDictionary {
7575
///
7676
/// - Complexity: Expected O(*n*) on average, where *n* is the count if
7777
/// key-value pairs, if `Key` implements high-quality hashing.
78+
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
7879
@inlinable
7980
public init<S: Sequence>(
8081
uniqueKeysWithValues keysAndValues: S
@@ -195,6 +196,7 @@ extension OrderedDictionary {
195196
///
196197
/// - Complexity: Expected O(*n*) on average, where *n* is the count of
197198
/// key-value pairs, if `Key` implements high-quality hashing.
199+
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
198200
@inlinable
199201
@inline(__always)
200202
public init<S: Sequence>(
@@ -368,6 +370,7 @@ extension OrderedDictionary {
368370
///
369371
/// - Complexity: Expected O(*n*) on average, where *n* is the count if
370372
/// key-value pairs, if `Key` implements high-quality hashing.
373+
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
371374
@inlinable
372375
public init<S: Sequence>(
373376
uncheckedUniqueKeysWithValues keysAndValues: S

Sources/OrderedCollections/OrderedDictionary/OrderedDictionary.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ extension OrderedDictionary {
785785
///
786786
/// - Complexity: Expected to be O(*n*) on average, where *n* is the number of
787787
/// elements in `keysAndValues`, if `Key` implements high-quality hashing.
788+
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
788789
@inlinable
789790
public mutating func merge<S: Sequence>(
790791
_ keysAndValues: __owned S,
@@ -880,6 +881,7 @@ extension OrderedDictionary {
880881
/// - Complexity: Expected to be O(`count` + *n*) on average, where *n* is the
881882
/// number of elements in `keysAndValues`, if `Key` implements high-quality
882883
/// hashing.
884+
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
883885
@inlinable
884886
public __consuming func merging<S: Sequence>(
885887
_ other: __owned S,

Tests/OrderedCollectionsTests/OrderedDictionary/OrderedDictionary Tests.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,5 +1290,75 @@ class OrderedDictionaryTests: CollectionTestCase {
12901290
}
12911291
}
12921292

1293+
func test_uniqueKeysWithValues_initializer_ambiguity() {
1294+
// https://github.com/apple/swift-collections/issues/125
1295+
1296+
let names = ["dylan", "bob", "aaron", "carol"]
1297+
let expected = names.map { (key: $0, value: 0) }
1298+
1299+
let d1 = OrderedDictionary(uniqueKeysWithValues: names.map { ($0, 0) })
1300+
expectEqualElements(d1, expected)
1301+
1302+
let d2 = OrderedDictionary(
1303+
uniqueKeysWithValues: ["dylan", "bob", "aaron", "carol"].map { ($0, 0) })
1304+
expectEqualElements(d2, expected)
1305+
1306+
let d3 = OrderedDictionary(
1307+
uniqueKeysWithValues: names.map { (key: $0, value: 0) })
1308+
expectEqualElements(d3, expected)
1309+
1310+
let d4 = OrderedDictionary(
1311+
uniqueKeysWithValues: ["dylan", "bob", "aaron", "carol"].map { (key: $0, value: 0) })
1312+
expectEqualElements(d4, expected)
1313+
}
1314+
1315+
func test_uniquingKeysWith_initializer_ambiguity() {
1316+
// https://github.com/apple/swift-collections/issues/125
1317+
let d1 = OrderedDictionary(
1318+
["a", "b", "a"].map { ($0, 1) },
1319+
uniquingKeysWith: +)
1320+
expectEqualElements(d1, ["a": 2, "b": 1] as KeyValuePairs)
1321+
1322+
let d2 = OrderedDictionary(
1323+
["a", "b", "a"].map { (key: $0, value: 1) },
1324+
uniquingKeysWith: +)
1325+
expectEqualElements(d2, ["a": 2, "b": 1] as KeyValuePairs)
1326+
}
1327+
1328+
func test_merge_ambiguity() {
1329+
// https://github.com/apple/swift-collections/issues/125
1330+
1331+
var d1: OrderedDictionary = ["a": 1, "b": 2]
1332+
d1.merge(
1333+
["c", "a"].map { ($0, 1) },
1334+
uniquingKeysWith: +
1335+
)
1336+
expectEqualElements(d1, ["a": 2, "b": 2, "c": 1] as KeyValuePairs)
1337+
1338+
var d2: OrderedDictionary = ["a": 1, "b": 2]
1339+
d2.merge(
1340+
["c", "a"].map { (key: $0, value: 1) },
1341+
uniquingKeysWith: +
1342+
)
1343+
expectEqualElements(d2, ["a": 2, "b": 2, "c": 1] as KeyValuePairs)
1344+
}
1345+
1346+
func test_merging_ambiguity() {
1347+
// https://github.com/apple/swift-collections/issues/125
1348+
1349+
let d1: OrderedDictionary = ["a": 1, "b": 2]
1350+
let d1m = d1.merging(
1351+
["c", "a"].map { ($0, 1) },
1352+
uniquingKeysWith: +
1353+
)
1354+
expectEqualElements(d1m, ["a": 2, "b": 2, "c": 1] as KeyValuePairs)
1355+
1356+
let d2: OrderedDictionary = ["a": 1, "b": 2]
1357+
let d2m = d2.merging(
1358+
["c", "a"].map { (key: $0, value: 1) },
1359+
uniquingKeysWith: +
1360+
)
1361+
expectEqualElements(d2m, ["a": 2, "b": 2, "c": 1] as KeyValuePairs)
1362+
}
12931363
}
12941364

0 commit comments

Comments
 (0)