title: Call specific function if one exists, otherwise the default from the protocol
body: <p>I have the following situation: there's a protocol version of a function and there might be a specific one of structs that implement that protocol. If a specific one exists, I want to call that one, otherwise the default one.</p>
  
  <p>This works if the type is most specific. But if I'm somewhere (like iterating over a collection) where just the protocol conformance is granted, what's the best way to check if a specific function exists?</p>
  
  <p>In the following I'm checking in the generic implementation if the instance is more special. But that's somewhat manual as I have to know myself which of the structs (..Agents) implements the function.</p>
  
  <p>Is there a better way to do this?</p>
  
  <pre><code>import Cocoa
  
  protocol Agent {
      func doSomething()
  }
  extension Agent {
      func doSomething() {
          if let specialAgent = self as? SpecialAgent {
              specialAgent.doSomething()
              return
          } // same for fooAgent, barAgent, ...
          print("extension implementation")
      }
  }
  
  struct SpecialAgent: Agent {
      func doSomething() {
          print("specific implementation")
      }
  }
  
  // this would be somewhere where we get all kinds of Agents and act on them
  let agents: [Agent] = [SpecialAgent()]
  for agent in agents {
      agent.doSomething()
  }
  </code></pre>