title: Implementing a registerClass like behavior in Swift using generics
body: <p>I have a tricky question to figure out. But I'm not able to find out a specific solution for it.</p>
  
  <p>Before explaining the goal this is the code I created so far.</p>
  
  <pre><code>protocol SomeProtocol {
  
      init()
      func parse()
  
  }
  
  class MyService&lt;T : SomeProtocol&gt; {
  
      var clazz : T.Type?
  
      func registerClass(clazz: T.Type) {
          self.clazz = clazz
      }
  
      func doWork() {
  
          if let clazz = self.clazz {
  
              let clazzInstance = clazz.init()
              clazzInstance.parse()
  
          }
      }
  
  }
  
  class MyClass : SomeProtocol {
  
      required init() { }
  
      func parse() {
          print("My Class parsing")
      }
  
  }
  
  class MyClass2 : SomeProtocol {
  
      required init() { }
  
      func parse() {
          print("My Class2 parsing")
      }
  
  }
  </code></pre>
  
  <p>The above code can be used like the following:</p>
  
  <pre><code>let myService = MyService&lt;MyClass&gt;()
  myService.registerClass(MyClass.self)
  myService.doWork()
  
  let myService2 = MyService&lt;MyClass2&gt;()
  myService2.registerClass(MyClass2.self)
  myService2.doWork() 
  </code></pre>
  
  <p>So, in order to explain a little bit about the context. I have a service, <code>MyService</code> in this case. I can create an instance of that service in order to perform some work. The service does a simple job: it receives some parameters and returns some JSON data. Based on the class I registered in the service (see <code>registerClass</code> method), the service will create an instance of that class and then it will invoke some method on it (in fact the class is registered conforms to <code>SomeProtocol</code> protocol). Here it is important to note that the code responsible to create the service class can choose to register or not a class. The <code>clazz</code> property is optional, as you can see.</p>
  
  <p>At this point, this is what I would like to achieve: removing the <code>T</code> at the class level and moving it at the <code>registerClass</code> level. In other words, like the following:</p>
  
  <pre><code>class MyService {
  
      var clazz : T.Type?
  
      func registerClass&lt;T : SomeProtocol&gt;(clazz: T.Type) {
          self.clazz = clazz
      }
  
      // the other code here
  
  }
  </code></pre>
  
  <p>Based on this assumption, the usage should become:</p>
  
  <pre><code>let myService = MyService()
  // I can choose to call this method in order to register a class into it
  myService.registerClass(MyClass.self) 
  myService.doWork()
  </code></pre>
  
  <p>Obviously if I do in this manner, this is the error I receive for the <code>class</code> property within <code>MyService</code> class.</p>
  
  <blockquote>
    <p>Use of undeclared type 'T'</p>
  </blockquote>
  
  <p>Any suggestions to reach my goal? Thanks.</p>
  
  <p>P.S. Feel free to modify the title. I haven't found a better one.</p>