# Property wrapper on a function's signature

**URL:** <https://forums.swift.org/t/property-wrapper-on-a-functions-signature/62660>\
**Category:** Using Swift\
**Created:** [January 19, 2023, 11:52am UTC](https://forums.swift.org/t/property-wrapper-on-a-functions-signature/62660 "2023-01-19T11:52:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 19, 2023, 11:52am UTC](https://forums.swift.org/t/property-wrapper-on-a-functions-signature/62660/1 "2023-01-19T11:52:14Z")

</div>

```swift
@propertyWrapper
final class Wrapper<T> {

    final class InnerWrapper {
        let val: T
        init(val: T) {self.val = val}
    }

    var storedValue: InnerWrapper

    var projectedValue: InnerWrapper {
        storedValue
    }

    var wrappedValue: T {
        self.storedValue.val
    }
    
    init(wrappedValue: T) {
        self.storedValue = InnerWrapper(val: wrappedValue)
    }
    
    init(projectedValue: Wrapper) {
        self.storedValue = projectedValue.storedValue
    }
}

```

in a type's body, it works fine:

```swift
class Base {
    @Wrapper // It works fine.
    var x = 0

    func run() {
        let y = x
        // foo(val: $x)
    }
}

```

when using it on a function, I got an error:  
`cannot convert value of type 'Wrapper<T>.InnerWrapper' to expected argument type 'Wrapper<T>'`

```swift
func foo<T>(@Wrapper val: T) {}

```

I think it is a bug??

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [January 19, 2023, 12:15pm UTC](https://forums.swift.org/t/property-wrapper-on-a-functions-signature/62660/2 "2023-01-19T12:15:04Z")

</div>

It might be a bug. But does it matter? The reason you're seeing it is because your `projectedValue` labeling is inaccurate.

There are two ways to solve it:

1. Don't use the argument label `projectedValue`.

```swift
init(_ projectedValue: Wrapper) {
  storedValue = projectedValue.storedValue
}

```

1. Don't use the obfuscation of `storedValue`, which isn't doing anything useful.

```swift
var wrappedValue: T { projectedValue.val }
var projectedValue: InnerWrapper

init(wrappedValue: T) {
  projectedValue = .init(val: wrappedValue)
}

init(_ wrapper: Wrapper) {
  projectedValue = wrapper.projectedValue
}

```

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [January 19, 2023, 6:28pm UTC](https://forums.swift.org/t/property-wrapper-on-a-functions-signature/62660/3 "2023-01-19T18:28:13Z")

</div>

> [@carlhung](#):
>
> when using it on a function, I got an error:  
> `cannot convert value of type 'Wrapper<T>.InnerWrapper' to expected argument type 'Wrapper<T>'`
> 
> ```swift
> func foo<T>(@Wrapper val: T) {}
> 
> ```
> 
> I think it is a bug??

The error message is not helpful, but the error is because your `init(projectedValue)` accepts the wrapper type instead of the projected value type. This fixes the problem:

```swift
@propertyWrapper
final class Wrapper<T> {

    final class InnerWrapper {
        let val: T
        init(val: T) {self.val = val}
    }

    var storedValue: InnerWrapper

    var projectedValue: InnerWrapper {
        storedValue
    }

    var wrappedValue: T {
        self.storedValue.val
    }

    init(wrappedValue: T) {
        self.storedValue = InnerWrapper(val: wrappedValue)
    }

    init(projectedValue: InnerWrapper) {
        self.storedValue = projectedValue
    }
}

func foo<T>(@Wrapper val: T) {}

```

It's worth filing a bug [Issues · apple/swift · GitHub](https://github.com/apple/swift/issues) to improve the error message!
