You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-8333 Compiler fails to disambiguate .map(Data.init) in Xcode 10 Beta 4
Issue Description:
In Xcode 9.4.1 with Swift 4.1.2 this compiles:
import Foundation
let string: String.UTF8View? = "Hi".utf8
_ = string.map(Data.init)
With Xcode 10 beta 4 and Swift 4.1.50 it produces this error:
foo.swift:4:16: error: ambiguous use of 'init'
_ = string.map(Data.init)
^
Foundation.Data:29:12: note: found this candidate
public init<S>(_ elements: S) where S : Sequence, S.Element == UInt8
^
Foundation.Data:30:12: note: found this candidate
public init<S>(bytes elements: S) where S : Sequence, S.Element == UInt8
^
Changing the code to this fixes it:
import Foundation
let string: String.UTF8View? = "Hi".utf8
_ = string.map { Data($0) }
The text was updated successfully, but these errors were encountered:
Hm. I'm not sure anything's going to change here; strictly maintaining source compatibility would mean never adding new overloads. cc @phausler, @rudkx, @airspeedswift
`init(bytes: Sequence)` and `init(_ elements: Sequence)` are not overloads in that they have two different names. It seems linguistically that fetching the init function should have a more qualified name: e.g. via argument labels as they are part of the signature fo the function.
Consider the following translation to objc:
+ (instancetype)dataWithSequence:(Sequence<UInt8> *)sequence;
// and the other method
+ (instancetype)dataWithBytes:(Sequence<UInt8> *)sequence;
// so fetching the IMP to pass to a mapping function would use two distinct selectors
[Data methodForSelector:@selector(dataWithSequence)];
[Data methodForSelector:@selector(dataWithBytes)];
So in the end it seems like there ought to be a way to specify
That's not how Swift works. "Overloads" means the same base name, differentiating first by argument names and then by types. That's important for default arguments.
(You certainly can write string.map(Data.init(_:)), and I kind of thing the current syntax shouldn't be legal myself, but it is. Still, I don't think that should stop us from adding new overloads, especially new initializers!)
Environment
Xcode 9.4.1 and Xcode 10 beta 4
Swift 4.1
Additional Detail from JIRA
md5: 138ae961b16b353bf87d2faead4c1242
is duplicated by:
Issue Description:
In Xcode 9.4.1 with Swift 4.1.2 this compiles:
With Xcode 10 beta 4 and Swift 4.1.50 it produces this error:
Changing the code to this fixes it:
The text was updated successfully, but these errors were encountered: