News for hackers. Stuff that matters.
Re:Swift as a C Language REPL by j_m_b on Thursday January 02, 2020 @09:42AM (#21935665)
So `let` is being used to define a var that is lexically scoped to... the global environment?! I've noticed you can do this in Javascript as well. As a Lisp programmer, its very jarring and seems like a bad practice. Is there a reason you would do this?
Re:Swift as a C Language REPL by dlivingston on Thursday January 02, 2020 @12:55PM (#21937781)
In Swift, IIRC, `let` defines a constant reference to a value within the current scope.
Re:Swift as a C Language REPL by hhas01 on Friday January 03, 2020 @10:59AM (#21946995)
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.)

Re:Swift as a C Language REPL by zapzupnz on Thursday January 02, 2020 @05:12PM (#21940505)
> As a Lisp programmer, its very jarring and seems like a bad practice.

As a programmer of any language, guessing at what something does, not verifying that the guess was right, and then complaining about it seems like a bad practice.

Re:Swift as a C Language REPL by jakear on Thursday January 02, 2020 @01:10PM (#21937968)
Only because the current scope was the global one.