[swift-users] Best way to solve ambiguous references to methods

Vinicius Vendramini vinivendra at gmail.com
Sat Dec 5 11:38:13 CST 2015


So I was playing around with Sequences, and I came across a problem I haven't been able to solve in a decent way. I’ve reduced it to a simpler example here:


let array : [Int] = [1, 2, 3]

let mapArray = array.map { $0 }

let flatArray = mapArray.flatten() // Error!


This last line of code prints the following error:


let flatArray = mapArray.flatten() // Error!
                ^~~~~~~~
Swift.SequenceType:4:17: note: found this candidate
    public func flatten() -> FlattenSequence<Self>
                ^
Swift.CollectionType:4:17: note: found this candidate
    public func flatten() -> FlattenCollection<Self>
                ^
Swift.CollectionType:4:17: note: found this candidate
    public func flatten() -> FlattenBidirectionalCollection<Self>
                ^


As far as I understand it, the error happens because several protocols extensions implement the `flatten` method, and `mapArray` (which is of type `Array<Optional<Int>>`) conforms to a few of those, which means the compiler has no way of knowing which one I intend on using.

How do I solve this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151205/d436439c/attachment.html>


More information about the swift-users mailing list