I’ve been exploring how to do dependency injection in conjunction with protocol oriented programming in Swift and wanted to propose an idea for a specific problem that I’ve ran into. I may be on completely the wrong path but as someone who previously worked a little in C#, there are many things I’m looking forward to being implemented in Swift’s generics system and I did not see this presently in any of the other discussions so I apologize if this is already on the list. My proposal would be to enhance the generics system so that the following can be done:
lazy var myObject: MyProtocol?
The problem I’m trying to solve is that I would like to be able to define a lot of my dependencies up front when the application first starts but I don’t necessarily want them to be instantiated until they actually get used to save some of the up front load up time and etc. These dependencies are all instantiated as prototypes so that I can have highly testable code with loose coupling. Because they are all defined as protocols, I am not presently able to make use of the `lazy` keyword because it requires the property have an initializer (i.e. that it be a struct or class). With this change I would then be able to accomplish that.
Let me know what you think or if you need more information!