gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 155 forks source link

`break` and `continue` don't work in `for..let` loops and consequences of that (discussion) #1024

Open pepkin88 opened 6 years ago

pepkin88 commented 6 years ago

TL;DR: There could be some mention about that fact in the documentation.


Because of how for..let loops wrap their bodies in the function, you cannot use break and continue in them. Hacking them to work, with labels, would be a bigger challenge, not impossible (Babel has it), but probably not worth the effort. That's why I believe that it is as intended, but I think this limitation should be documented.

However, one of the bummers in ES5 were, that you couldn't at the same time use break or continue and be having an iteration scoped variables, like in Array.prototype.map, at the same time. So the ES6 added lets and consts. But because in LS they are emulated using ES5 tricks, they come with ES5 limitations. And perhaps there are more use cases, where JS lets make more sense, than LS helpers, though nothing comes to my mind at the moment.

And I also don't think it is super important. I would probably find a simple way to get around such issue in my code.

In summary, options "what to do":

I would opt for the first one or the Babel one, but that's another issue (#821?).

EDIT: Later I thought that using ``let ii = i;`` could bypass the problem. EDIT2: Later I thought that using a simple loop with let i block just for a function could bypass the problem with just LS mechanisms. Okay, disregard the "(discussion)" part, this issue is now just for the sake of a mention in the documentation.

mt4c commented 4 years ago

also return do not break the loop and return a value