what's different?

The professor codes like this in the lecture

var number: Int { return 3 }

what's different about this?

var number: Int = 3

Answered by Claude31 in 720293022

In practice number is 3 when you use it, such as

let newNumber = number

Behind the scene it is quite different.

var number: Int = 3

You assign a value of 3 to the var. You can later change its value at will. number = 4

var number: Int { return 3 }

Is a so called computed var When you get the var, you execute the closure. Test this

var number: Int { 
  print("I'm number 3")
  return 3
}

So you can have a more complex logic:

var three = true

var number: Int { 
  print("I'm number 3")
  return three ? 3 : 4
}
print(number)

You get 3

If you now set

three = false
and call
print(number)

you get 4.

This is very convenient: you can now use number without worrying about var as three : everything is encapsulated in the var itself.

In the same way you can define a setter, to update another var (like an IBOutlet) when the value changes. See details in Swift Reference manual.

Note that you cannot assign anymore a new value directly:

number = 4 

will cause error: Cannot assign to value: 'number' is a get-only property. Which shows that such a use is not really useful. It should in such a case be simpler to declare as a const:

let number = 3

The reason is that computed var is just that: a var computed from another var.

For instance, if you want to compute miles per gallon, you will have

var miles : Float = 100
var gallons : Float = 4
var mpg : Float {
   return gallons > 0 ? miles / gallons : 0
}

Get more here: https://stackoverflow.com/questions/29690521/set-value-to-computed-properties-in-swift

Accepted Answer

In practice number is 3 when you use it, such as

let newNumber = number

Behind the scene it is quite different.

var number: Int = 3

You assign a value of 3 to the var. You can later change its value at will. number = 4

var number: Int { return 3 }

Is a so called computed var When you get the var, you execute the closure. Test this

var number: Int { 
  print("I'm number 3")
  return 3
}

So you can have a more complex logic:

var three = true

var number: Int { 
  print("I'm number 3")
  return three ? 3 : 4
}
print(number)

You get 3

If you now set

three = false
and call
print(number)

you get 4.

This is very convenient: you can now use number without worrying about var as three : everything is encapsulated in the var itself.

In the same way you can define a setter, to update another var (like an IBOutlet) when the value changes. See details in Swift Reference manual.

Note that you cannot assign anymore a new value directly:

number = 4 

will cause error: Cannot assign to value: 'number' is a get-only property. Which shows that such a use is not really useful. It should in such a case be simpler to declare as a const:

let number = 3

The reason is that computed var is just that: a var computed from another var.

For instance, if you want to compute miles per gallon, you will have

var miles : Float = 100
var gallons : Float = 4
var mpg : Float {
   return gallons > 0 ? miles / gallons : 0
}

Get more here: https://stackoverflow.com/questions/29690521/set-value-to-computed-properties-in-swift

One more point. You can define a setter and then change the number.

But each time you access to number, you will get the original value of 3:

var number: Int {
    get {
        print("I'm number 3")
        return 3
    }
    set(newValue) {
        print("number", number)
    }
}

number = 4
print(number)

gives:

number 4
I'm number 3
3

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.

what's different?
 
 
Q