UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: what is a constant ?

Forums > 100 Days of Swift

I'm currently at day8 ... so having worked through lots of test-cases I found a lot of examples like (syntax is not correct and shortend )

for 1..3 { let myVar = func() .....

the let statement will be called three times ... and gets different values all the time.

In my old world of other languages ... a constant is a constant

It is hard for me to develop a sense for this use. Why not making an honest variable

3      

It is a constant. It's just that each iteration through the loop creates a new scope (delineated by the { } brackets), so the myVar created during the first time through the loop is a different constant than the myVar created during the second time through the loop, and so on.

But within each iteration of the loop, myVar is a constant value.

3      

Understood....

but still the question - why not using an honest variable like

var myVar: [..]
for 1..3 { 
      myVar = func() .....

what are the motivation points: a) performance? b) one additional line for var declaration c) having now an additional line with type information?

3      

The basic distinction is simple: if you set the value and then it never changes afterwards, it's a constant (let). If you want to be able to vary the value, it's a variable (var).

But - why not just make everything a var? It's about safety (mainly), and also performance.

There are lots of cases where we want to be able to set a value that can never change during its lifetime. For a trivial example, suppose I write a function that squares numbers. I might write something like

let squared = number * number

For a particular number, I want to set the value of squared once, and then never change it, so I use a let. I could use a var, and in a trivial example like this it might make little practical difference. But in a real app we would be introducing a potential problem - somewhere down the line, in some dark, deep corner of code, I could inadvertently change the value of something that was never meant to change, and cause a problem. Using let and var appropriately avoids this whole class of errors.

There is also a performance gain from using let wherever possible - the compiler can optimise things if it knows a value is never going to change.

3      

It entirely depends on what that loop is doing. It's hard to give a better answer without knowing what the rest of the code looks like.

In general, constants are better than variables because they are safer (i.e., they can't accidentally be changed and screw something up somewhere). But they can also be more efficient.

To quote Paul:

Having both these options might seem pointless, after all you could just create a variable then never change it – why does it need to be made a constant? Well, it turns out that many programmers are – shock! – less than perfect at programming, and we make mistakes.

One of the advantages of separating constants and variables is that Xcode will tell us if we've made a mistake. If we say, "make this date a constant, because I know it will never change" then 10 lines later try to change it, Xcode will refuse to build our app.

Constants are also important because they let Xcode make decisions about the way it builds your app. If it knows a value will never change, it is able to apply optimizations to make your code run faster.

I'm not exactly sure what you mean by "an honest variable". The code snippet you suggest should only be used if there is some need for a value that can be changed by whatever is going on within the loop. Even then, depending on what you are doing, you could possibly do it a different way and keep the value a constant.

For instance:

var randomInts1: [Int] = []
for _ in 1...10 {
    randomInts1.append(Int.random(in: 100...200))
}

//same thing can be accomplished with a constant
let randomInts2 = (1...10).map { _ in Int.random(in: 100...200) }

But, really, it all depends on what you are doing and what you need the value for as to whether you declare it as a var or a let.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.