<div dir="ltr">New here, hope I'm doing this right.
<div><br></div><div>I recently found a use case which does not appear to be covered by the swift language: it appears that there's no way to declare a closure type with generic arguments.</div><div><br></div><div>For example, what if I'm building an iOS app, and I want to define a closure which operates on UIViewControllers which implement a particular protocol?<br></div><div><br></div><div>In ObjectiveC, this would be possible with the following syntax:</div><div><br></div><div> typedef void (^MyBlockType)(UIViewController<MyProtocol>)</div><div><br></div><div>In Swift you can do this in the context of a function, class or struct:</div><div><br></div><div> func myFunction<T: UIViewController where T:MyProtocol>() {</div><div> let x : (T) -> () = { generic in</div><div> ....</div><div> }</div><div> }</div><div><br></div><div>But what if you want to declare such a closure as a variable within the protocol it's referring to?</div><div><br></div><div> protocol MyProtocol {</div><div> var protocolControllerCallback : (protocolController : ???? )->()</div><div> }</div><div><br></div><div>It seems like Associated Types are the intended tool for this job, so you could have something like:</div><div><br class=""> protocol MyProtocol {</div><div> typealias ProtocolObservingController</div><div> var protocolControllerCallback : (protocolController : ProtocolObservingController )->() {get set}</div><div> }</div><div><br></div><div> class MyController : UIViewController, MyProtocol {</div><div> typealias ProtocolObservingController = MyController</div><div> ...</div><div> }</div><div><br></div><div>But this doesn't quite solve the problem: we still can't have a closure defined in this protocol which can operate on *any* implementations of the protocol. For instance, what if we wanted to pass a closure between two different implementations?</div><div><br></div><div> class MyChildController : UIViewController, MyProtocol {</div><div> typealias ProtocolObservingController = MyChildController</div><div> ...</div><div> }</div><div><br></div><div><div> class MyParentController : UIViewController, MyProtocol {</div><div> typealias ProtocolObservingController = MyParentController</div><div> ...</div><div><br></div><div> func createChildController() -> MyControllerChild {</div><div> let child = MyChildController()</div><div> child.protocolControllerCallback = self.protocolControllerCallback</div><div> }</div><div><br></div><div> }</div></div><div><br></div><div>It doesn't work because the types of protocolControllerCallback are inconsistent between the two classes.</div><div><br></div><div>This feels like a hole in the language.</div><div><br></div><div><br></div><div><br></div><div><br></div></div>