Have this class which is never used directly, only inherited:
class ApiBase {}
How do I define a generic static singleton default
here? - So I can:
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()
The only thing I can think of is to create a protocol
which FooApi
and BarApi
need to implement. But that seems suboptimal, would prefer to write an implementation like:
func `default`<T: ApiBase>() -> T {
if staticInstance == nil {
staticInstance = T()
}
return staticInstance
}