title: How do I cast the argument type within a closure signature in Swift?
body: <p>I'm trying to write a light observer class in Swift (currently Swift 2). The idea is to use it within an Entity Component system, as a means for the components to communicate with one-another without being coupled together. </p>
  
  <p>The problem I'm having is that all types of data could be communicated, a <code>CGVector</code>, an <code>NSTimeInterval</code> and so on. This means that the method being passed could have all kinds of type signatures <code>(CGVector) -&gt; Void</code>, <code>() -&gt; Void</code> etc.</p>
  
  <p>I'd like to be able to store these varying signatures in an array, but still have some type safety. My thinking is that the type for the array would be <code>(Any) -&gt; Void</code> or perhaps <code>(Any?) -&gt; Void</code>, so that I can at least ensure that it contains methods. But I'm having trouble passing methods in this way: <code>Cannot convert value of type '(CGVector) -&gt; ()' to expected argument type '(Any) -&gt; ()'</code>. </p>
  
  <h3>First attempt:</h3>
  
  <pre><code>//: Playground - noun: a place where people can play
  
  import Cocoa
  import Foundation
  
  enum EventName: String {
      case input, update
  }
  
  struct Binding{
      let listener: Component
      let action: (Any) -&gt; ()
  }
  
  class EventManager {
      var events = [EventName: [Binding]]()
  
      func add(name: EventName, event: Binding) {
          if var eventArray = events[name] {
              eventArray.append(event)
          } else {
              events[name] = [event]
          }
      }
  
      func dispatch(name: EventName, argument: Any) {
          if let eventArray = events[name] {
              for element in eventArray {
                  element.action(argument)
              }
          }
      }
  
      func remove(name: EventName, listener: Component) {
          if var eventArray = events[name] {
              eventArray = eventArray.filter(){ $0.listener.doc  != listener.doc }
          }
      }
  }
  
  // Usage test
  
  //Components
  
  protocol Component {
      var doc: String { get }
  }
  
  class Input: Component {
      let doc = "InputComponent"
      let eventManager: EventManager
  
      init(eventManager: EventManager) {
          self.eventManager = eventManager
      }
  
      func goRight() {
          eventManager.dispatch(.input, argument: CGVector(dx: 10, dy: 0) )
      }
  }
  
  class Movement: Component {
      let doc = "MovementComponent"
  
      func move(vector: CGVector) {
          print("moved \(vector)")
      }
  
  }
  
  class Physics: Component {
      let doc = "PhysicsComponent"
  
      func update(time: NSTimeInterval){
          print("updated at \(time)")
      }
  }
  
  
  class someClass {
      //events
      let eventManager = EventManager()
  
      // components
      let inputComponent: Input
      let moveComponent = Movement()
  
      init() {
          inputComponent = Input(eventManager: eventManager)
  
          let inputBinding = Binding(listener: moveComponent, action: moveComponent.move) // ! Cannot convert value of type '(CGVector) -&gt; ()' to expected argument type '(Any) -&gt; ()'
          eventManager.add(.input, event: inputBinding)
  
      }
  }
  
  let someInstance = someClass()
  someInstance.inputComponent.goRight()
  </code></pre>
  
  <p>Throws the error <code>Cannot convert value of type '(CGVector) -&gt; ()' to expected argument type '(Any) -&gt; ()'</code>. </p>
  
  <h3>Second attempt</h3>
  
  <p>If I genericize the <code>Binding</code> struct to recognise different types of arguments I have a bit more luck. This version basically works, but the array holding the methods is now <code>[Any]</code> ( I'm not sure whether it's the attempt to cast <code>Any</code> back to the <code>Binding</code> struct that is causing the slightly odd error below <code>Binary operator '!=' cannot be applied to two 'String' operands</code>):</p>
  
  <pre><code>struct Binding&lt;Argument&gt;{
      let listener: Component
      let action: (Argument) -&gt; ()
  }
  
  class EventManager {
      var events = [EventName: [Any]]()
  
      func add(name: EventName, event: Any) {
          if var eventArray = events[name] {
              eventArray.append(event)
          } else {
              events[name] = [event]
          }
      }
  
      func dispatch&lt;Argument&gt;(name: EventName, argument: Argument) {
          if let eventArray = events[name] {
              for element in eventArray {
                  (element as! Binding&lt;Argument&gt;).action(argument)
              }
          }
      }
  
      func remove(name: EventName, listener: Component) {
          if var eventArray = events[name] {
             // eventArray = eventArray.filter(){ ($0 as! Binding).listener.doc  != listener.doc } //Binary operator '!=' cannot be applied to two 'String' operands
          }
      }
  }
  </code></pre>
  
  <p>Is there a way to do this and have the array hold methods of varying type signatures, something like <code>[(Any?) -&gt; ()]</code> ?</p>
  
  <h3>Attempt 3...</h3>
  
  <p>Reading around, eg here <a href="http://www.klundberg.com/blog/capturing-objects-weakly-in-instance-method-references-in-swift/" rel="noreferrer">http://www.klundberg.com/blog/capturing-objects-weakly-in-instance-method-references-in-swift/</a> it seems that my approach above will lead to strong reference cycles, and that what I need to do is pass the static method eg <code>Movement.move</code> rather than <code>moveComponent.move</code>. So the type signature I would be storing would actually be <code>(Component) -&gt; (Any?) -&gt; Void</code> rather than <code>(Any?) -&gt; Void</code>. But my question still stands, I still would like to be able to store an array of these static methods with a bit more type-safety than just <code>[Any]</code>.</p>