Comments (6)
The range
function works fine, the example that reports different n
than what you'd expect is a compile-time optimization RapydScript does to the logic. Technically it is a bug (I will fix it), but it's not the range function that is broken, what's happening is n
gets incremented at the end of the loop, if the loop was rewritten as below, this bug could be avoided:
for (n = 0; n <= 5-1; n++) {
_$rapyd$_print(n);
}
On a related note, it's a bad practice to use n
outside of the loop after it terminates, the fact that it doesn't get reset is a side-effect of Python's scoping. RapydScript simply emulates that, pure JavaScript destroys the variable declared inside the loop when it's done.
from rapydscript.
pure JavaScript destroys the variable declared inside the loop when it's done.
Are you sure? I am new to javascript (in fact I have yet to write a line of it!) so I may be mistaken, but from what I've just read, javascript's scope is either global or by function, not by block. I have seen several stackoverflow answers that say this, and I also tried your example above in jsfiddle and I can still access n outside of the for loop - even if I stick a "var" in front of the "n=0" .
There is a new (since 2009?) "let" keyword that can do what you say, but it does not seem to me that the variable is destroyed unless you go out of your way to do so (with let keyword).
On a related note, it's a bad practice to use n outside of the loop after it terminates,
I am curious why you say that. What you said above ( for-loop scope ) is true in Lua, and that seemed like a wart to me. I agree it's bad to pollute the global namespace, but I don't see the harm in the way python keeps it around.
Lastly,
for (n = 0; n <= 5-1; n++) {
_$rapyd$_print(n);
}
I tried your fixed example above, and it still has n = 5 after the for loop is completed.
Off topic, just wanted to say - I am very excited about Rapydscript - I have put off learning Javascript for nearly 20 years, turned off by the C-like syntax. Rapydscript "feels" right, without the bloat of the other python-in-browser projects - Thank you for writing it!
from rapydscript.
Yes, if the variable is properly declared inside the loop (as below), it will be destroyed:
for (var n in range(5)) {
...
}
If you forget var
, then it will in fact be global, but that's not a proper declaration in JavaScript, these "features" that encourage JavaScript developers to shoot themselves in the foot is why alternatives such as CoffeeScript and RapydScript exist. You are correct that a variable declared within the loop body itself will be local to the containing function, not the loop, but the iterator declared in conditional will be local to the loop. Try the following, for example:
for (var n in [0,1,2,3,4]) {
console.log(n);
}
console.log(n);
I just tested the example, and you're right that it doesn't fix it, I guess I was wrong about order of operations in the conditional check, I'll fix it by decrementing it at the end of the loop instead.
I'm glad you like RapydScript, that's exactly the same motivation I had for writing it, after playing with some of the more bloated alternatives. I encourage you to join our mailing list as well: https://groups.google.com/forum/?fromgroups#!forum/rapydscript
from rapydscript.
Yes, if the variable is properly declared inside the loop (as below), it will be destroyed:
...
the iterator declared in conditional will be local to the loop. Try the following, for example:
I did try this, see: http://jsfiddle.net/j22d4/
and n is still alive (not destroyed), so I am not sure what you mean...?
I will check out the mailing list! Thanks
from rapydscript.
You're right, I was testing it incorrectly.
from rapydscript.
Hmm, rather than fixing this I think I'll have the linter prevent this usage. Using loop-specific variables outside of a loop is poor design - and in fact not possible in many other languages including perl (and soon won't be possible in native JavaScript with the addition of let
keyword). If you need to access the variable after the loop terminates, better technique is to copy the value to another variable before loop finishes.
from rapydscript.
Related Issues (20)
- append method does not compile to js push method HOT 1
- VSCode extension for syntax highlighting HOT 4
- cannot compare string HOT 3
- Typescript has problem compile RS --es6 output at class attributes HOT 2
- Why not support in-browser compiler? HOT 2
- enhancement: check for already reserved words like: enum HOT 18
- Cannot resolve python imports HOT 8
- Is that possible to parse python code to js AST? HOT 1
- fstring support. HOT 1
- How to keep all comments HOT 8
- Can't find python module that is installed and available? HOT 1
- Cannot catch ImportError HOT 2
- Cannot import from custom module in different directory HOT 5
- How to use RapydScript with JavaScript MutationObserver HOT 1
- Question about feasibility: option to use JS function syntax for nested functions, especially inside dicts/object-literals HOT 7
- How to run code snippets from tutorials on old Android? HOT 1
- Computed and negative slices
- parenthesized syntax HOT 2
- TypeError: undefined is not a function HOT 1
- Compiler issues HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rapydscript.