How to access the wrapped value of a @Published property when passing to generic function

Viewed 491

When I pass a property which is declared as @Published to a generic function, I get an error referring to the published value. It's fine anywhere else where I use the variable, just not when passing into the generic function.

Error:

error: cannot convert value 'user' of type 'User?' to expected type 'Published<User?>.Publisher?', use wrapper instead

Here's the playground code:

struct User {}

class Test {
    @Published var user: User?
}

func normalFunc(_ argument: User?) -> User? {
    return argument
}

func genericFunc<T>(_ argument: T?) -> T? {
    return argument
}

let test = Test()
normalFunc(test.user) // Ok
genericFunc(test.user) // Error

The best I have been able to do is wrap the value in parenthesis, like so:

genericFunc((test.user)) // Ok!
1 Answers
Related