title: Swift inheritance of singleton pattern?
body: <p>Have this class which is never used directly, only inherited:</p>
  
  <pre><code>class ApiBase {}
  </code></pre>
  
  <p>How do I define a generic static singleton <code>default</code> here? - So I can:</p>
  
  <pre><code>class FooApi: ApiBase {}  // Want only one instance of `FooApi`
  class BarApi: ApiBase {}  // Want only one instance of `BarApi`
  
  FooApi.default.foo_api_only_method_name()
  BarApi.default.bar_api_only_method_name()
  </code></pre>
  
  <p>The only thing I can think of is to create a <code>protocol</code> which <code>FooApi</code> and <code>BarApi</code> need to implement. But that seems suboptimal, would prefer to write an implementation like:</p>
  
  <pre><code>func `default`&lt;T: ApiBase&gt;() -&gt; T {
      if staticInstance == nil {
          staticInstance = T()
      }
      return staticInstance
  }
  </code></pre>