4 ms·
Correct. `let` = constant binding, `var` = variable binding, and except for top-level (global) declarations they are always block scoped. BUT, there is a gotch
by hhas01 7y ago
Correct. `let` = constant binding, `var` = variable binding, and except for top-level (global) declarations they are always block scoped.
BUT, there is a gotcha: this “constantness” only applies to the name-value binding, not to the value itself. Thus you can still mutate the content of a let-bound value when that value is a class or class instance. Which kinda defeats the point of having the let/var distinction in the first place. C’s `const` may have its priorities backwards, but least it does what it says on the tin.
(And let’s not mention what a PITA Swift gets when you want to work with the contents of complex nested struct-based collections in a language that doesn’t have native pointers/references. Again, it makes C look good.)