Reputation: 69
I would like to clarify if I understand the following concept correctly.
Assume that my goal is to store the String
"Good morning, Mike"
into the variable var sayGoodMorningToUser
.
The String is composed of two variables, namely
var greeting = "Good morning, "
var username = "Mike"
What difference does it make if I use stored properties vs. computed properties, in other words:
var sayGoodMorningToUserStored = greeting + username
vs.
var sayGoodMorningToUserComputed:String {
return greeting + username
}
The only difference I see between these two approaches is that anyone could change the value of sayGoodMorningToUserStored easily and directly, e.g. by writing
var sayGoodMorningToUserStored = "myNewChangedValue"
whereas the variable sayGoodMorningToUserComputed cannot be modified directly, because it cannot simply be set it to a new String value:
var sayGoodMorningToUserComputed = "Hallo" //this would cause an error
Otherwise I cannot understand why people compute the variable instead of simply writing
var sayGoodMorningToUserStored = greeting + username.
Can anyone explain if I understood it correctly? Or are there also other advantages of computed variables vs. stored ones?
I would like to limit my question to gettable variables only, because discussing settable variables here would go beyond the scope.
Upvotes: 0
Views: 1234
Reputation: 7746
The difference in your example is this:
var sayGoodMorningToUserStored = "myNewChangedValue"
and
var sayGoodMorningToUserStored = greeting + username
are set when your class is initialized, whereas this:
var sayGoodMorningToUserComputed:String {
return greeting + username
}
is evaluated every time the property is accessed.
The simple example is a class that has a firstName
and lastName
, but also want fullName
. Using a normal property, every time you update firstName
or lastName
you will also have to update fullName
so it will match. Using a computed property, every time you access fullName
you will get the up to date information.
Upvotes: 3
Reputation: 63157
var sayGoodMorningToUserComputed: String {
return greeting + username
}
sayGoodMorningToUserComputed
acts just like a function. If a change has been made to greeting
or username
, then sayGoodMorningToUserComputed
will return an up-to-date result that will be the concatenation of the current values.
You would want to use this if you want to ensure your returned value is computed off the latest values of its dependencies (greeting
and username
).
In the case that both dependencies are final
, then it's very likely that the compiler would optimise this computed property into a stored property, because it knows the dependencies can't change
var sayGoodMorningToUserStored = greeting + username
sayGoodMorningToUserStored
is just a variable, with nothing special going on. However, it's only set once, whenever the containing scope is initialized. It's computed once, stored and remains constant until it is overwritten by an external source. As such, if greeting
or username
changes, there will be no effect on sayGoodMorningToUserStored
, because it's been computed from the old values, and stored.
You would want to use this if you want to improve performance by caching the result of a computation whose dependencies are constant.
Upvotes: 1