Expose property name to property wrapper

I've seen a common pattern among property wrappers that persist the property's value to use Strings as unique identifiers. Most recently with the introduction of @SceneStorage and @AppStorage in SwiftUI.

@AppStorage("preferredColorScheme") var preferredColorScheme: ColorScheme = .dark

I was wondering if it would be at all possible to expose the name of a property to its property wrapper. This would not only result in less verbosity, but also benefits from the compiler ensuring unique names across a specific scope. So the code from above would become

@AppStorage var preferredColorScheme: ColorScheme = .dark
22 Likes

It feels like a small yet quality-of-life feature👀 Is there any reason we shouldn’t introduce a new magic string #property for the wrappers?

7 Likes

100% agree, this would be useful for any kind of database wrappers, types that deal with codable, user defaults etc.

Would be a nice addition for sure... maybe included as one of the initializer options?

.init(wrappedValue: Value, _propertyName: String?)

I think the property name would have to be nil if used within a function.