[swift-users] Error creating empty array of chained associated type

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Sat Apr 29 15:38:40 CDT 2017


I’ve stumbled upon an odd situation where Swift gives a compiler error if I
do things directly, but works properly with no error if I first create a
typealias. Here is a stripped-down example:

protocol P {
    associatedtype C: Collection
}

extension P {
    func emptyArray() -> [C.Iterator.Element] {
        return [C.Iterator.Element]()   // Error
    }
}

The “return” line gives the error “Cannot call value of non-function type
'[Self.C.Iterator.Element.Type]'” in Xcode 8.3.2. However, if we replace
that function with a seemingly-equivalent version that uses a typealias,
there is no error:

extension P {
    func emptyArray() -> [C.Iterator.Element] {
        typealias E = C.Iterator.Element
        return [E]()    // Works
    }
}

Is this a known bug?

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170429/ae982b72/attachment.html>


More information about the swift-users mailing list