Skip to content

[Foundation] handle ambiguity in swift 4 mode for Data.init with byte sequences #18443

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 2 commits into from
Aug 2, 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
11 changes: 11 additions & 0 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1248,11 +1248,22 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
self.init(backing: backing, range: 0..<backing._length)
}

@available(swift, introduced: 4.2)
@inlinable
public init<S: Sequence>(bytes elements: S) where S.Iterator.Element == UInt8 {
self.init(elements)
}

@available(swift, obsoleted: 4.2)
public init(bytes: Array<UInt8>) {
self.init(bytes)
}

@available(swift, obsoleted: 4.2)
public init(bytes: ArraySlice<UInt8>) {
self.init(bytes)
}

@usableFromInline
internal init(backing: _DataStorage, range: Range<Index>) {
_backing = backing
Expand Down
35 changes: 35 additions & 0 deletions test/stdlib/TestData_Swift4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -swift-version 4 %s -o %t/TestData_Swift4
// RUN: %target-run %t/TestData_Swift4
// REQUIRES: executable_test
// REQUIRES: objc_interop

import Foundation
import StdlibUnittest

var DataTests = TestSuite("TestDataSwift4")

DataTests.test("functional map init usage") {
let res1 = [[UInt8(0), UInt8(1), UInt8(2)]].map(Data.init) // previously this could be done without being ambiguous (however in swift 4.2 an overload was added that makes it ambiguous as a function ref)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I had in mind, thanks!

// the following two strategies are preferred to the previous version
let res2 = [[UInt8(0), UInt8(1), UInt8(2)]].map(Data.init(_:))
let res3 = [[UInt8(0), UInt8(1), UInt8(2)]].map { Data($0) }

expectEqual(res1.count, 1)
expectEqual(res2.count, 1)
expectEqual(res3.count, 1)

expectEqual(res1[0], res2[0])
expectEqual(res2[0], res3[0])
}

runAllTests()