What's different? Here's what's different. If you write:
var number: Int = 3
Then the next person to look at this code will have no trouble understanding what it is doing.
On the other hand, if you write this:
var number: Int { return 3 }
then the only people who fully understand it will be swift experts. And today, as Swift is a relatively new language, there is a very good chance that the next person who looks at your code will not be an expert. In fact, you should assume that the next person who tries to understand your code will be a psychopath who knows where you live. So avoid not-syntactically-obvious features unless you have a very good reason to use them!
What you should do if you want a variable-like-thing whose value is computed when it is used, is use the slightly more verbose "get" syntax that claude shows at the end of his post, which I think is equivalent (someone correct me if I'm wrong):
var number: Int {
get {
return 3
}
}
The additional syntax there gives the psychopathic future reader a clue what is going on - return 3 is a statement executed in the getter for this not-a-normal-variable thing - so they should have deciphered what your code does before they arrive at your house with their axe.
Topic:
Programming Languages
SubTopic:
Swift
Tags: