Skip to content

[stdlib] Rearrange + on RangeReplaceableCollection/Sequence once again #13736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 35 additions & 38 deletions stdlib/public/core/RangeReplaceableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -903,36 +903,6 @@ extension RangeReplaceableCollection
}

extension Sequence {
/// Creates a new collection by concatenating the elements of a collection and
/// a sequence.
///
/// The two arguments must have the same `Element` type. For example, you can
/// concatenate the elements of an integer array and a `Range<Int>` instance.
///
/// let numbers = [1, 2, 3, 4]
/// let moreNumbers = numbers + 5...10
/// print(moreNumbers)
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
///
/// The resulting collection has the type of the argument on the left-hand
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
/// which is `[Int]`.
///
/// - Parameters:
/// - lhs: A range-replaceable collection.
/// - rhs: A collection or finite sequence.
@_inlineable
public static func + <
Other : RangeReplaceableCollection
>(lhs: Other, rhs: Self) -> Other
where Element == Other.Element {

var lhs = lhs
// FIXME: what if lhs is a reference type? This will mutate it.
lhs.append(contentsOf: rhs)
return lhs
}

/// Creates a new collection by concatenating the elements of a sequence and a
/// collection.
///
Expand All @@ -956,13 +926,43 @@ extension Sequence {
Other : RangeReplaceableCollection
>(lhs: Self, rhs: Other) -> Other
where Element == Other.Element {

var result = Other()
result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount))
result.append(contentsOf: lhs)
result.append(contentsOf: rhs)
return result
}
}

extension RangeReplaceableCollection {
/// Creates a new collection by concatenating the elements of a collection and
/// a sequence.
///
/// The two arguments must have the same `Element` type. For example, you can
/// concatenate the elements of an integer array and a `Range<Int>` instance.
///
/// let numbers = [1, 2, 3, 4]
/// let moreNumbers = numbers + 5...10
/// print(moreNumbers)
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
///
/// The resulting collection has the type of the argument on the left-hand
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
/// which is `[Int]`.
///
/// - Parameters:
/// - lhs: A range-replaceable collection.
/// - rhs: A collection or finite sequence.
@_inlineable
public static func + <
Other : Sequence
>(lhs: Self, rhs: Other) -> Self
where Element == Other.Element {
var lhs = lhs
// FIXME: what if lhs is a reference type? This will mutate it.
lhs.append(contentsOf: rhs)
return lhs
}

/// Appends the elements of a sequence to a range-replaceable collection.
///
Expand All @@ -982,14 +982,12 @@ extension Sequence {
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
@_inlineable
public static func += <
Other : RangeReplaceableCollection
>(lhs: inout Other, rhs: Self)
Other : Sequence
>(lhs: inout Self, rhs: Other)
where Element == Other.Element {
lhs.append(contentsOf: rhs)
}
}

extension RangeReplaceableCollection {
/// Creates a new collection by concatenating the elements of two collections.
///
/// The two arguments must have the same `Element` type. For example, you can
Expand All @@ -1009,11 +1007,10 @@ extension RangeReplaceableCollection {
/// - lhs: A range-replaceable collection.
/// - rhs: Another range-replaceable collection.
@_inlineable
public static func +<
public static func + <
Other : RangeReplaceableCollection
>(lhs: Self, rhs: Other) -> Self
where Element == Other.Element {

where Element == Other.Element {
var lhs = lhs
// FIXME: what if lhs is a reference type? This will mutate it.
lhs.append(contentsOf: rhs)
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func rdar19831698() {
var v72 = true + true // expected-error{{binary operator '+' cannot be applied to two 'Bool' operands}}
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists:}}
var v73 = true + [] // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and '[Any]'}}
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists:}}
// expected-note @-1 {{expected an argument list of type '(Self, Other)'}}
var v75 = true + "str" // expected-error {{binary operator '+' cannot be applied to operands of type 'Bool' and 'String'}} expected-note {{expected an argument list of type '(String, String)'}}
}

Expand Down