0

So here is my playground, i would love to use the openInitial111() function but it just wont compile, the openInitial222() is working perfectly, whats the problem ?

protocol Module : Screen {
   init()
}
extension Module {
   static func entity() -> Self {
      return Self()
   }
}
protocol Screen {

}
class FFF: Module {
   required init() {
   }
}
class ZZZ: NSObject {
    func openInitial111(initialModule: Module.Type) {
        self.openViewController(itemWithScreenStyle: initialModule.entity())  // Error : Cannot invoke openViewController
                                                                          //with an argument list of type '(initWithScreenStyle:Module)
    }

    func openInitial222(initialModule: Module.Type) {
       let f = FFF.self  // FFF.Type
       self.openViewController(itemWithScreenStyle: f.entity())  // works
    }


    func openViewController<T: Screen>(itemWithScreenStyle: T) {
        print("generics")
    }
}
Ammo
  • 570
  • 1
  • 8
  • 22
  • [Protocols don't conform to themselves in Swift](http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself) – therefore you cannot use `Module` as a type that conforms to `Screen`, you need `openViewController`'s `T` to be a concrete type (which is why it works with `FFF`). One possible way to make it work with `Module` would be to build a type erasure, such as shown in Rob's answer to the linked Q&A. – Hamish Nov 07 '16 at 11:32

1 Answers1

0

Indeed as suggested in comments the underlaying issue is that protocol doesn't conform to protocol. The error message is incredibly misleading though.

Ammo
  • 570
  • 1
  • 8
  • 22