title: Defining explicit conversion for custom types in Swift
body: <p>What is currently the best/preferred way to define explicit conversions in Swift? Of the top of my head I can think of two:</p>
  
  <ul>
  <li><p>Creating custom initializers for the destination type via an extension, like this:</p>
  
  <pre><code>extension String {
    init(_ myType: MyType) {
      self = "Some Value"
    }
  }
  </code></pre>
  
  <p>This way, you could just use <code>String(m)</code> where m is of type <code>MyType</code> to convert m to a string.</p></li>
  <li><p>Defining <code>toType</code>-Methods in the source type, like this:</p>
  
  <pre><code>class MyType {
    func toString() -&gt; String {
      return "Some Value"
    }
  }
  </code></pre>
  
  <p>This is comparable to Swift's <code>String.toInt()</code>, which returns an <code>Int?</code>. But if this was the definitive way to go I would expect there to be protocols for the basic types for this, like an inversion of the already existing <code>*LiteralConvertible</code> protocols.</p></li>
  </ul>
  
  <p>Sub-question: None of the two methods allow something like this to compile: <code>let s: MyString = myTypeInstance (as String)</code> (part in parentheses optional), but if I understand right, the <code>as</code> operator is only for downcasting within type hierarchies, is that correct?</p>