Skip to content

Commit dbf4bf9

Browse files
committed
Move += operator for append(contentsOf:) up to RangeReplaceableCollection
1 parent 04c7d94 commit dbf4bf9

File tree

4 files changed

+28
-61
lines changed

4 files changed

+28
-61
lines changed

stdlib/public/core/ArrayType.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ internal protocol _ArrayProtocol
4343
/// - Complexity: O(`self.count`).
4444
mutating func reserveCapacity(_ minimumCapacity: Int)
4545

46-
/// Operator form of `append(contentsOf:)`.
47-
static func += <S : Sequence>(lhs: inout Self, rhs: S)
48-
where S.Iterator.Element == Iterator.Element
49-
5046
/// Insert `newElement` at index `i`.
5147
///
5248
/// Invalidates all indices with respect to `self`.

stdlib/public/core/Arrays.swift.gyb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,30 +1739,6 @@ extension ${Self} {
17391739
}
17401740
}
17411741
}
1742-
1743-
// FIXME(ABI)#16 : remove this entrypoint. The functionality should be provided by
1744-
// a `+=` operator on `RangeReplaceableCollection`.
1745-
/// Appends the elements of a sequence to ${a_Self}.
1746-
///
1747-
/// Use this operator to append the elements of a sequence to the end of
1748-
/// ${a_Self} with same `Element` type. This example appends
1749-
/// the elements of a `Range<Int>` instance to an array of integers.
1750-
///
1751-
/// var numbers = [1, 2, 3, 4, 5]
1752-
/// numbers += 10...15
1753-
/// print(numbers)
1754-
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
1755-
///
1756-
/// - Parameters:
1757-
/// - lhs: The array to append to.
1758-
/// - rhs: A collection or finite sequence.
1759-
///
1760-
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
1761-
public func += <
1762-
S : Sequence
1763-
>(lhs: inout ${Self}<S.Iterator.Element>, rhs: S) {
1764-
lhs.append(contentsOf: rhs)
1765-
}
17661742
% end
17671743

17681744
//===--- generic helpers --------------------------------------------------===//

stdlib/public/core/RangeReplaceableCollection.swift.gyb

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,6 @@ public protocol RangeReplaceableCollection
294294
with newElements: C
295295
) where C : Collection, C.Iterator.Element == Iterator.Element
296296

297-
/*
298-
We could have these operators with default implementations, but the compiler
299-
crashes:
300-
301-
<rdar://problem/16566712> Dependent type should have been substituted by Sema
302-
or SILGen
303-
304-
func + <S : Sequence>(_: Self, _: S) -> Self
305-
where S.Iterator.Element == Iterator.Element
306-
307-
func + <S : Sequence>(_: S, _: Self) -> Self
308-
where S.Iterator.Element == Iterator.Element
309-
310-
func + <S : Collection>(_: Self, _: S) -> Self
311-
where S.Iterator.Element == Iterator.Element
312-
313-
func + <RC : RangeReplaceableCollection>(_: Self, _: S) -> Self
314-
where RC.Iterator.Element == Iterator.Element
315-
*/
316-
317297
/// Prepares the collection to store the specified number of elements, when
318298
/// doing so is appropriate for the underlying type.
319299
///
@@ -370,16 +350,6 @@ public protocol RangeReplaceableCollection
370350
/// collection.
371351
mutating func append(_ newElement: Iterator.Element)
372352

373-
/*
374-
The 'append(contentsOf:)' requirement should be an operator, but the compiler crashes:
375-
376-
<rdar://problem/16566712> Dependent type should have been substituted by Sema
377-
or SILGen
378-
379-
func += <S : Sequence>(inout _: Self, _: S)
380-
where S.Iterator.Element == Iterator.Element
381-
*/
382-
383353
/// Adds the elements of a sequence or collection to the end of this
384354
/// collection.
385355
///
@@ -398,6 +368,8 @@ public protocol RangeReplaceableCollection
398368
///
399369
/// - Complexity: O(*n*), where *n* is the length of the resulting
400370
/// collection.
371+
// FIXME(ABI)#166 (Evolution): Consider replacing .append(contentsOf) with +=
372+
// suggestion in SE-91
401373
mutating func append<S : Sequence>(contentsOf newElements: S)
402374
where S.Iterator.Element == Iterator.Element
403375

@@ -1171,6 +1143,29 @@ public func +<
11711143
return lhs
11721144
}
11731145

1146+
/// Appends the elements of a sequence to a range-replaceable collection.
1147+
///
1148+
/// Use this operator to append the elements of a sequence to the end of
1149+
/// range-replaceable collection with same `Element` type. This example appends
1150+
/// the elements of a `Range<Int>` instance to an array of integers.
1151+
///
1152+
/// var numbers = [1, 2, 3, 4, 5]
1153+
/// numbers += 10...15
1154+
/// print(numbers)
1155+
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
1156+
///
1157+
/// - Parameters:
1158+
/// - lhs: The array to append to.
1159+
/// - rhs: A collection or finite sequence.
1160+
///
1161+
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
1162+
public func += <
1163+
R : RangeReplaceableCollection, S : Sequence
1164+
>(lhs: inout R, rhs: S)
1165+
where R.Iterator.Element == S.Iterator.Element {
1166+
lhs.append(contentsOf: rhs)
1167+
}
1168+
11741169
@available(*, unavailable, renamed: "RangeReplaceableCollection")
11751170
public typealias RangeReplaceableCollectionType = RangeReplaceableCollection
11761171

test/Misc/misc_diagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ func test17875634() {
9999
var col = 2
100100
var coord = (row, col)
101101

102-
match += (1, 2) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
102+
match += (1, 2) // expected-error{{argument type '(Int, Int)' does not conform to expected type 'Sequence'}}
103103

104-
match += (row, col) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
104+
match += (row, col) // expected-error{{argument type '(@lvalue Int, @lvalue Int)' does not conform to expected type 'Sequence'}}
105105

106-
match += coord // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
106+
match += coord // expected-error{{argument type '@lvalue (Int, Int)' does not conform to expected type 'Sequence'}}
107107

108108
match.append(row, col) // expected-error{{extra argument in call}}
109109

0 commit comments

Comments
 (0)