title: Check if variable is a block / function / callable in Swift
body: <p>Is there a simple and definite way in Swift to check whether something is a callable block / function? In some languages it's a trivial thing, but perhaps I'm looking at this from a wrong perspective in Swift? Consider the following.</p>
  
  <pre><code>func foo(){ print("foo") }
  var bar: () -&gt; () = { print("bar") }
  var baz: () -&gt; (Bool) = { print("baz"); return true }
  
  print(foo) // (Function)
  print(bar) // (Function)
  print(baz) // (Function)
  
  print(foo is () -&gt; ()) // true
  print(bar is () -&gt; ()) // true
  print(baz is () -&gt; ()) // false
  print(baz is () -&gt; (Bool)) // true
  </code></pre>
  
  <p>Swift knows that they are all functions, though there is no such data type. I can check by using a solid signature, but there might be a situation where I don't care about the signature<sup>*</sup> and simply want to invoke it. For example:</p>
  
  <pre><code>func call(callable: () -&gt; ()) {
      callable()
  }
  
  call(foo) // foo
  call(bar) // bar
  call(baz) // error: cannot convert value of type '() -&gt; (Bool)' to expected argument type '() -&gt; ()'
  </code></pre>
  
  <p>I can rewrite it like this, which will work for <code>Void</code> and <code>Bool</code> return types, but doing this for every type is crazy, especially since I don't care about it, but compiler does…</p>
  
  <pre><code>func call(callable: Any) {
      if let block: () -&gt; () = callable as? () -&gt; () {
          block()
      } else if let block: () -&gt; (Bool) = callable as? () -&gt; (Bool) {
          block()
      }
  }
  
  call(foo) // foo
  call(bar) // bar
  call(baz) // truely baz
  </code></pre>
  
  <p><sup>*</sup> Agree, not caring about the signature is a sin. For the argument sake let's just not care about the return type.</p>