What a constant is
A constant is a name bound to a value that cannot change after it is set. You declare one like a variable, but with the keyword let instead of var:
let companyName = "Acme"
print(companyName)
Acme
Reading a constant works exactly like reading a variable. You can print it, use it in calculations and interpolate it into strings. The only thing you cannot do is assign to it again.
What happens when you try to change one
let ourName = "Acme"
ourName = "Andy"
error: cannot assign to value: 'ourName' is a 'let' constant
note: change 'let' to 'var' to make it mutable
This is a compile-time error, so the program never starts. The same applies to compound operators: with let maxLives = 3, the line maxLives += 1 fails with left side of mutating operator isn't mutable: 'maxLives' is a 'let' constant.
Why limit yourself?
A constant looks like a variable with fewer abilities, and that is the point. When you see let, you know the value is the same everywhere it is used, so you do not have to scan the rest of the code for places that might change it. In a large program with many authors, the compiler enforces that promise.
Use let for anything that is set once: a company name, the value of pi, the maximum number of lives in a game. Use var only for values that must change, such as a score or a counter. Swift programmers write let by default and switch to var when the compiler or the design demands it.
The exercise, step by step
The starter code declares pi as a constant, then tries to overwrite it:
let pi = 3.14
pi = 5.0
print(pi)
Because pi is a let, the line pi = 5.0 is rejected and the program cannot be built. The goal is to print 3.14, so the fix is to delete that line.
- Find the reassignment
pi = 5.0. - Delete it. Do not change
lettovar: that would compile but print5.0, and the value of pi was meant to be fixed. - Keep the declaration and the
printline exactly as they are.
The solution in Swift
let pi = 3.14
print(pi)
The program prints 3.14. The blank lines left by the deleted line are harmless.
Swift details
- The value is not always known upfront. A constant may be declared first and assigned once later, as in
let later: Intfollowed bylater = 5. It can be assigned only once, and the type must be written because there is no value to infer from. - A constant collection cannot be modified. With
let list = [1, 2], callinglist.append(3)fails withcannot use mutating member on immutable value: 'list' is a 'let' constant. - The type is still inferred.
let pi = 3.14is aDouble. - Naming. Constants follow the same camelCase naming as variables. Swift has no ALL_CAPS convention for them.
Common mistakes
- Changing
lettovarjust to silence the error. Ask whether the value should change first. - Using
varfor everything. Avarthat is never changed suggests a value that might change when it does not. - Assuming
lethides a value. You can read a constant anywhere it is in scope.
Key takeaways
letdeclares a constant that cannot be reassigned.- Assigning to a constant is a compile-time error, so mistakes are caught before the program runs.
- Default to
letand usevaronly when a value has to change.
