breuleux / earl-grey

Programming language compiling to JavaScript
http://breuleux.github.io/earl-grey/
MIT License
464 stars 6 forks source link

`let` variables immutable? #7

Open davej opened 9 years ago

davej commented 9 years ago

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.

breuleux commented 9 years ago

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 9 years ago

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.

MadcapJake commented 9 years ago

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.