Why "let/var" and not "const/var" in Swift?

Hi there! Out of curiosity. Why not use const and var instead of let and var in Swift? I'm intrigued because it seems easier to understand var as variable and const as constant. I'm a JavaScript developer, and I never understood choosing let as the new var, but in Swift, choosing let as the new const... well, it's a little bit confusing. Is there a genuine reason for this decision, or was it to avoid colliding with JavaScript? Thanks a lot in advance for your answers 🙂

Answered by Claude31 in 700021022

There is a very subtle difference between a let in Swift and a const in other languages.

const is initialised at compile time, let may be initialised at runtime and then immutable (with a closure for instance).

So, let is wider use than const.

See very interesting discussion here: https://stackoverflow.com/questions/26063763/why-is-a-constant-declared-with-the-keyword-let-in-swift

Accepted Answer

There is a very subtle difference between a let in Swift and a const in other languages.

const is initialised at compile time, let may be initialised at runtime and then immutable (with a closure for instance).

So, let is wider use than const.

See very interesting discussion here: https://stackoverflow.com/questions/26063763/why-is-a-constant-declared-with-the-keyword-let-in-swift

Thanks a lot, @Claude31. I get the concept: there is not a real const in Swift. That's why Swift uses let. To avoid confusion with a traditional constant value.

You made my day! Happy 2022 😄

Why "let/var" and not "const/var" in Swift?
 
 
Q