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