title: How to use Self for method return types in Swift?
body: <p>This code produces Xcode error messages that lead you in a circle. Let's say I have a protocol called <code>Marker</code> and I'd like Markers to copy themselves. Here's a first guess...</p>
  
  <pre><code>protocol Marker {
      func copy() -&gt; Self
  }
  class Marker1 : Marker {
      func copy() -&gt; Self {
          return Marker1()   // error here
      }
  }
  </code></pre>
  
  <p>(I'm not sure how to use <code>Self</code> correctly, because I can't find it in the <em>The Swift Programming Language</em> document. If you know where it's documented, please include that in answer.)</p>
  
  <p>That code gives an error on the marked line: <code>Cannot convert return expression of type 'Marker1' to return type 'Self'</code> and it suggests a fix: <code>Insert ' as! Self'</code>. </p>
  
  <p>I accept the fix:</p>
  
  <pre><code>...
      return Marker1() as! Self
  ...
  </code></pre>
  
  <p>This leads to another compiler error: <code>'Self' is only available in a protocol or as the result of a method in a class; did you mean 'Marker1'?</code></p>
  
  <p>If I accept that "fix" it goes back to the original error. I'd call that a bug in Xcode. Let's try something else:</p>
  
  <pre><code>func copy() -&gt; Marker1 {
      return Marker1()
  }
  </code></pre>
  
  <p>Another error: <code>Method 'copy()' in non-final class 'Marker1' must return `Self` to conform to protocol 'Marker'</code></p>
  
  <p>Making the class <code>final</code> does fix the error. But is there a way to do this without making a class final? And where is <code>Self</code> documented?</p>