title: Swift rewriting init(format: String, _ arguments: CVarArg...)
body: <p>I'm trying to rewrite the standard String(format, arguments) method found in foundation which takes a string and replaces all values containing %i with ints and %@ with strings and a whole range of types. <a href="https://developer.apple.com/documentation/swift/string/3126742-init" rel="nofollow noreferrer">https://developer.apple.com/documentation/swift/string/3126742-init</a></p>
  
  <p>Since I don't know c I converted the initializer from </p>
  
  <pre><code>init(format: String, _ arguments: CVarArg) {
  </code></pre>
  
  <p>to </p>
  
  <pre><code>init(format: String, _ arguments: [Any]) {
  </code></pre>
  
  <p>Now I got it working for Ints using this in a String Extension</p>
  
  <pre><code>  init(format: String, _ arguments: [Any]) {
              var copy = format
              for argument in arguments {
                  switch argument {
                  case let replacementInt as Int:
                      String.handleInt(copy: &amp;copy, replacement: String(replacementInt))
                  default:
                      self = format
                  }
              }
          self = copy
      }
  
  private static func handleInt(copy: inout String, replacement: String) {
  </code></pre>
  
  <p>but since I would like this to work for all values I'm trying to use the switch to look for type Any that has the Protocol of LosslessStringConvertible required to convert to a string using the String(value) initializer.</p>
  
  <pre><code>     init(format: String, _ arguments: [Any]) {
          var copy = format
          for argument in arguments {
              switch argument {
              case let replacementInt as LosslessStringConvertible:
                  String.handleAnyValue(copy: &amp;copy, replacement: String(replacementInt))
              default:
                  self = format
              }
          }
      self = copy
  }
  </code></pre>
  
  <p>However, I get the following error when applying String(replacementInt)</p>
  
  <blockquote>
    <p>Protocol type 'LosslessStringConvertible' cannot conform to
    'LosslessStringConvertible' because only concrete types can conform to
    protocols</p>
  </blockquote>
  
  <p><strong>Bonus</strong>
  A bonus would be if I could do this without importing any library and simply writing using swift.</p>