title: Implementing a function with a default parameter defined in a protocol
body: <p>Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the <strong>default implementation is only used as a "fallback"</strong>: It's executed when a type conforms to the protocol but doesn't provide its own implementation.</p>
  
  <p>At least that's how I read <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267" rel="noreferrer">The Swift Programming Language</a> guide:</p>
  
  <blockquote>
    <p>If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension. </p>
  </blockquote>
  
  <p>Now I ran into a situation where my custom type that implements a certain protocol <em>does</em> provide an implementation for a particular function but it's not executed — the implementation defined in the protocol extension is executed instead.</p>
  
  <hr>
  
  <p><strong>As an example</strong>, I define a protocol <code>Movable</code> that has a function <code>move(to:)</code> and an extension that provides a default implementation for this function: </p>
  
  <pre><code>protocol Movable {
  
      func move(to point: CGPoint)
  
  }
  
  extension Movable {
  
      func move(to point: CGPoint = CGPoint(x: 0, y: 0)) {
          print("Moving to origin: \(point)")
      }
  
  }
  </code></pre>
  
  <p>Next, I define a <code>Car</code> class that conforms to <code>Movable</code> but provides its own implementation for the <code>move(to:)</code> function:</p>
  
  <pre><code>class Car: Movable {
  
      func move(to point: CGPoint = CGPoint(x: 0, y: 0)) {
          print("Moving to point: \(point)")
      }
  
  }
  </code></pre>
  
  <p>Now I create a new <code>Car</code> and downcast it as a <code>Movable</code>:</p>
  
  <pre><code>let castedCar = Car() as Movable
  </code></pre>
  
  <p>Depending on whether I pass a value for the optional parameter <code>point</code> I observe two distinct behaviors:</p>
  
  <hr>
  
  <ol>
  <li><p><strong>When passing a point</strong> for the optional parameter </p>
  
  <p>→ <strong>the <code>Car</code>'s implementation is called</strong>:</p>
  
  <pre><code>castedCar.move(to: CGPoint(x: 20, y: 10)) 
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <blockquote>
    <p>Moving to point: (20.0, 10.0)</p>
  </blockquote></li>
  </ol>
  
  <hr>
  
  <ol start="2">
  <li><p>When I invoke the <code>move()</code> function <strong>without providing a value for the optional parameter</strong> the <code>Car</code>'s implementation is ignored and</p>
  
  <p>→ <strong>the <code>Movable</code> protocol's default implementation is called instead</strong>:</p>
  
  <pre><code>castedCar.move()
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <blockquote>
    <p>Moving to origin: (0.0, 0.0)</p>
  </blockquote></li>
  </ol>
  
  <hr>
  
  <h1>Why?</h1>