breuleux/earl-grey

`let` variables immutable?

Opened this issue · 3 comments

davej commented

In the documentation it says that the following code gives an immutable variable:

let y = "hello"    ;; const variable (immutable)

It compiles to the equivalent javascript and let is not immutable in javascript, only const is immutable.

You are right, but it doesn't make any difference in practice because trying to set an immutable variable is a compile-time error in EG. Though, I should probably generate const anyway, if that can somehow help browsers run the code faster down the line.

davej commented

Ah ok, I didn't realise that it was enforced at compile time.

Yeah, I guess I was also just confused that the semantics of var and let are completely different than semantics of var and let in Javascript. I see that's also how it's done in Swift (and probably other languages), I've just never come across those semantics before.

According to this, const does cause JS compilation to change but the answer is a bit outdated. However, with ES6 finalized, I think it's definitely a good idea to generate ES6 files with it.