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

**URL:** <https://forums.swift.org/t/propertywrapper-with-simple-initializer-as-methods-argument-should-behave-like-a-default-argument/73290>\
**Category:** Pitches\
**Tags:** init, propertywrapper\
**Created:** [July 19, 2024, 9:17am UTC](https://forums.swift.org/t/propertywrapper-with-simple-initializer-as-methods-argument-should-behave-like-a-default-argument/73290 "2024-07-19T09:17:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lursk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lursk/32/36532_2.png) [@lursk](https://forums.swift.org/u/lursk)\
**Post date:** [July 19, 2024, 9:17am UTC](https://forums.swift.org/t/propertywrapper-with-simple-initializer-as-methods-argument-should-behave-like-a-default-argument/73290/1 "2024-07-19T09:17:28Z")

</div>

It's possible to create property wrappers that don't require a value to be set from outside. For example:

```swift
@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:

```swift
class ViewModel {
    @Inject var someService: SomeService

```

However, when trying to use the property wrapper in the initializer like this:

```swift
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:

```swift
init(@Inject someService: SomeService = Inject<SomeService>().wrappedValue)

```

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [July 19, 2024, 4:13pm UTC](https://forums.swift.org/t/propertywrapper-with-simple-initializer-as-methods-argument-should-behave-like-a-default-argument/73290/2 "2024-07-19T16:13:12Z")

</div>

> [@lursk](#):
>
> Since it has an initializer without wrappedValue

I think that initializer is better expressed as

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

```

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

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

```

but I agree with you completely.
