Michael Long
Jan 7, 2023

--

A little funny that you decided to use type inference for resolution, but ignored it for registration in favor of explicit type specification. A bit cumbersome, that.

Your registration mechanism is also somewhat problematic in that you're instantiating each and every instance of the desired class on application launch, and, by doing so, effectively making each and every object a singleton.

Better would be to define register<T>(factory @escaping: () -> T).

This provides a factory closure that creates objects on demand, when needed and whose type can be inferred from the return type of the closure.

See: https://github.com/hmlongco/Factory

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)