propertyWrapper with simple initializer as method's argument should behave like a default argument

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)
1 Like

I think that initializer is better expressed as

init(wrappedValue: TService = Container.shared.resolve(TService.self)

and you don't need to explicitly type Inject here:

init(@Inject someService: SomeService = Inject().wrappedValue)

but I agree with you completely.