Generics 为什么编译器会抱怨我将参数转换为FlatMap?

Generics 为什么编译器会抱怨我将参数转换为FlatMap?,generics,swift3,extension-methods,xcode8,Generics,Swift3,Extension Methods,Xcode8,我已经定义了一个协议和一个数组扩展。编译器在扩展的encode方法中调用flatMap时报告错误:无法将类型“T”的值转换为闭包结果类型“\uz” public protocol Encodable { typealias Properties = Dictionary<String, Any> func encode() -> Properties init?(_ properties: Properties?) } extension Array

我已经定义了一个协议和一个数组扩展。编译器在扩展的encode方法中调用flatMap时报告错误:无法将类型“T”的值转换为闭包结果类型“\uz”

public protocol Encodable {
    typealias Properties = Dictionary<String, Any>
    func encode() -> Properties
    init?(_ properties: Properties?)
}

extension Array where Element : Encodable.Properties {
    func encode<T:Encodable>(type: T.Type) -> [T] {
        return flatMap{ T($0) } // <= Compiler Error
    }
}
公共协议可编码{
typealias属性=字典
func encode()->属性
初始化?(uu属性:属性?)
}
扩展数组,其中元素:Encodable.Properties{
函数编码(类型:T.type)->[T]{

return flatMap{T($0)}/在一个小项目中编译代码时,我会发现另一个错误:

请尝试使用
flatMap(T.init)
。这不仅更可取,而且可能会导致更有用的错误消息
<unknown>:0: error: type 'Element' constrained to non-protocol type 'Encodable.Properties'
extension Array where Element == Encodable.Properties {
    func encode<T:Encodable>(type: T.Type) -> [T] {
        return flatMap{ T($0) }
    }
}