-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ranges don't allow the use of variables in 0.5.4 #65
Comments
They are never supposed to work dynamically--the syntax is
|
In that case, maybe they should produce a syntax error a little more explicit ? Like "SyntaxError: unexpected identifier 'b'" or something. Wouldn't that be interesting to add expression support to the ranges ? |
Perhaps. Patches welcome.
What's wrong with that? |
I'll try to find some time to write a patch. Nothing is wrong with .slice, it's just that it feels kind of sad that we can't use the nice sugar with variables when it seems natural we could. See python's [ ] operator for instance Anyway, my idea of implementation is the following : a[1 to b]
function () { var _res = []; for (var i = 0; i < b; i++) _res.push(a[i]); return _res; }() |
But
Even though |
I like coco better than python :) Besides, the JS way doesn't matter much here I suppose, since coco and CS exist for the sole purpose of redefining a better way.
That's just a matter of "beauty", really. I just find it elegant to be able to do it in a slice directly, rather than calling a function. I feel it's more "about Arrays". Plus I don't feel it's that complicated to include it in coco, and neither does it revolutionize anything in its use. IE, I think Just more sugar. Besides, the parsing is already (almost) here !
I really think that it would be a worthwile addition. I'll try to provide a patch, whenever I'm done getting used to the Jison syntax :) |
I don't get it. You are calling a function either way, and your version is doing it much less efficiently.
Disagreed. For reference, see coffee#746 for why they were removed in the first place.
The numeric ranges as we have now don't involve Jison parser. They are expanded within lexer. |
From my last comment, I would only call the function in the case where it's not possible to be efficient ; that is when using variables. .slice can be used when it's only I agree with ".." being ambiguous. However, the "from .. to .. by" syntax is not. Anyway, I know this is subjective, but I think What I'm proposing is only an addition ; it is perfectly possible to implement it so that the actual implementation is fully preserved in terms of efficiency, and it adds other possibilities. For exemple,
Ah well that's a little more complicated then. |
Alright, I'll try to summarize in a clearer way what I'm proposing : the current case is that when I have When having And when having function(){
var _res=[];
var _rep=null;
for(var i=0; i<(_rep = arr).length; ++i)
_res.push(_rep[i]);
return _res;
} or desugar to |
I have to agree that seeing the desugaring method, I can understand that this is not completely essential. A clearer syntax error would be a perfectly satisfactory solution. Sorry for spamming ! |
Was about to say "that case is rare enough that
|
I've got to say that I still like having the choice between one and another, especially since the proposed syntax is (a little) more concise. Anyway yeah, it would be nice to see it someday, although really, really not essential. |
So basically there seems to be a problem with using variables in ranges.
The text was updated successfully, but these errors were encountered: