[swift-users] Best way to solve ambiguous references to methods
Harlan Haskins
harlan at harlanhaskins.com
Sat Dec 5 11:43:13 CST 2015
A quick-and-dirty way would be to explicitly cast to the protocol type before calling the function.
let flatArray = (mapArray as SequenceType).flatten()
Or in this case you could just mapArray.flatMap()
> On Dec 5, 2015, at 12:38 PM, Vinicius Vendramini <vinivendra at gmail.com> wrote:
>
> 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?
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151205/ad44222e/attachment.html>
More information about the swift-users
mailing list