It's possible to create property wrappers that don't require a value to be set from outside. For example:
@propertyWrapper
struct Inject<TService> {
private(set) var wrappedValue: TService
init(wrappedValue: TService) {
self.wrappedValue = wrappedValue
}
init() {
self.wrappedValue = Container.shared.resolve(TService.self)
}
}
Since it has an initializer without wrappedValue, it can be used without explicitly setting its value:
class ViewModel {
@Inject var someService: SomeService
However, when trying to use the property wrapper in the initializer like this:
class ViewModel {
let someService: SomeService
init(@Inject someService: SomeService) {
self.someService = someService
}
}
the initializer forces us to set a value. Ideally, if the simple initializer is available, it should behave like a default value:
init(@Inject someService: SomeService = Inject<SomeService>().wrappedValue)