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

Andrey Fidrya af at zabiyaka.com
Wed Feb 1 08:51:21 CST 2017


> Update your where clause to:
>         where Type: PluginDataProtocol, Type.Parent == Monster {


Thank you, '==' works! I tried "Type.Parent: Monster" before which didn't compile.

Regards,
Andrey


> On 1 Feb 2017, at 00:27, Hooman Mehr <hooman at mac.com> wrote:
> 
> Update your where clause to:
> 
>         where Type: PluginDataProtocol, Type.Parent == Monster {
> 
> 
>> On Jan 31, 2017, at 10:36 AM, Andrey Fidrya via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> 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
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170201/17e41cf7/attachment.html>


More information about the swift-users mailing list