Why Property Wrapper can not be applied on Global or Computed variable?

Viewed 238

I was going through Swift Documentation and it says

You can apply a property wrapper to a local stored variable, but not to a global variable or a computed variable.

I know it is by design but just curious to know what could be the reason behind not applying the property wrapper to global or computed variable.

1 Answers

I don't have an answer for why a property wrapper cannot be applied to a global variable, but the answer to why it cannot be applied to a computed variable is given on the same page you were quoting:

Because property wrapper syntax is just syntactic sugar for a property with a getter and a setter.

To define a computed variable, you define a getter (and potentially a setter). Property wrapper also has a getter and a setter. You cannot have multiple getters and setters for the same variable, hence you cannot use a property wrapper for computed variable, which already has a getter / setter.

Besides, a computed property doesn't have a storage of its own.

Related