Error creating empty array of chained associated type


#1

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


(Martin R) #2

That seems to be a known problem: https://bugs.swift.org/browse/SR-773

···

On 29. Apr 2017, at 22:38, Nevin Brackett-Rozinsky via swift-users <swift-users@swift.org> wrote:

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
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users