For such complex use-cases and also to help port AS3 code it would be useful to support traditional for() loop syntax. Is this possible and are there any plans for this?
It should, as long as the multiple iterators are separated by semicolons. It looks like it copies the relevant expressions verbatim and appends a final semicolon.
Complicated. But I get the point. I never noticed that for loops need to have the semis replaced with commas for the extension to work. Thanks for the help!
Ordinarily, I wouldn’t want to do things to venture away from standard Haxe syntax, but in this case, I sort of wonder if we should consider using something like David’s macro (or yours, @player_03, without a tink_macro dependency) in OpenFL by default. I wonder if there is a way to avoid the macro build, and to more generally apply to user code.
Using @for instead of for obviously requires an opt-in. For this reason, it might not be wrong to include in OpenFL to make it a bit closer to ActionScript in this regard
This one isn’t so easy, unfortunately. I tried using Context.onGenerate() to add the build metadata, but unfortunately it crashes before the callback is called. (If only it was called earlier, the macro would fix the error.)
I’ll try looking through all class paths instead, and building the list of classes myself. (Perhaps excluding any that start with “haxe.”)
I suppose that brute-force would be a find/replace as a part of the OpenFL build process, but that would definitely begin to be far-reaching (like @for to Special.for)
Another idea would be something such as Lib.for ()
It’s already a brute-force find and replace within the files in question, so… What we really need is a way to “register” metadata, and have the compiler call a listener whenever “@for” is found.
At this point, Lib.for() might be the way to go. (Edit: Or something with a different name, because for is reserved.)
My own version is dangerous because it won’t always throw an error if you forget the @:build tag. For instance, this runs but only traces “0”:
Doesn’t help the people who are porting from AS3 or whatever, but it’s not a bad idea in general. I went and wrote an implementation of Python’s range() function. Check it out here.
And in case you’re wondering, the macros allow it to turn this:
for(i in Iterators.range(10, 5, -2)) {
//...
}
…into this:
var _g_currentValue = 12;
var _g_end = 6;
var _g_step = -2;
while ((_g_currentValue != _g_end)) {
var i = _g_currentValue += _g_step;
//...
};
…without a single function call making it into the final code!
Hmm, that’s tricky. I see two perspectives. One says that the first argument is always inclusive, the second is not. The other says that you include the lower value, not the higher.
I think if it was something like Iterators.reverse (0, 5) then I would do 0-4, but Iterators.from (5, 0) should be 5-1, I think, for consistency