[swift-users] Generic initializer's parameter type not being resolved

Andrey Fidrya af at zabiyaka.com
Tue Jan 31 12:36:47 CST 2017


Hi All,

I've encountered a very strange problem where generic initializer's parameter type isn't resolved correctly unless explicitly casted.

Below is playground code for reproducing the problem and compiler error I'm getting.
After removing "as! Type.Parent" cast the code won't compile anymore...

It expects Type.Parent and I'm passing Monster, but Parent is typealiased to Monster.
Why can't it infer the type automatically?


public protocol PluginDataProtocol: class {
    associatedtype Parent
    init(parent: Parent)
}

public class Monster {
    public var pluginsData = [ObjectIdentifier: AnyObject]()
    
    public func pluginData<Type>(id: ObjectIdentifier = ObjectIdentifier(Type.self)) -> Type
                where Type: PluginDataProtocol {
        if let data = pluginsData[id] as? Type {
            return data
        } else {
            let data = Type(parent: self as! Type.Parent)
            pluginsData[id] = data
            return data
        }
    }
}

final class MyMonsterPluginData: PluginDataProtocol {
    typealias Parent = Monster
    
    weak var parent: Parent?
    
    init(parent: Parent) {
        self.parent = parent
    }
}

Compiler error:

Playground execution failed: error: Generics.playground:14:24: error: cannot invoke initializer for type 'Type' with an argument list of type '(parent: Monster)'
            let data = Type(parent: self /* as! Type.Parent */)
                       ^

Generics.playground:14:24: note: expected an argument list of type '(parent: Self.Parent)'
            let data = Type(parent: self /* as! Type.Parent */)


Regards,
Andrey



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170131/3a181250/attachment.html>


More information about the swift-users mailing list