Step expression must be constant?

BlitzMax Forums/BlitzMax Beginners Area/Step expression must be constant?

koekjesbaby(Posted 2005) [#1]
Why does the step part of a for/next loop has to be constant? I find this seriously annoying, because is means that i have to rewrite something simple as:
For local x = 0 to width step gridsize

to:
Local x
While x < width
    x :+ gridsize
Wend

It might be a small detail, but I really don't see any reason for the step part to be constant...

Also the help documents say:
Keyword To Followed by a constant which is used to calculate when to exit a For..Next loop.
But the To keyword, luckily, doesn't need to be followed by a constant.


Perturbatio(Posted 2005) [#2]
As I understand it, one of the reasons it has to be constant is because if you were to modify gridsize, it would probably end up throwing an exception (you could end up referencing beyond the end of an array for instance).


koekjesbaby(Posted 2005) [#3]
true, but there are loads of constructions which you can use to create errors which doesn't mean that those constructions should be limited.

besides, i (wrongfully?) assume that, for the compiler, the step expression works like the while/wend construction above.


Perturbatio(Posted 2005) [#4]
I think it's also for compiler optimization actually, I seem to recall a discussion about this issue elsewhere (maybe for B3D).

*EDIT*

here we are:
http://www.blitzbasic.com/archive/posts.php?topic=18836


koekjesbaby(Posted 2005) [#5]
ah, i see, mark is being anal ;) but it seems to make sense so i'll just shut it and use while/wend instead.


Robert(Posted 2005) [#6]
I think it's also for compiler optimization actually, I seem to recall a discussion about this issue elsewhere (maybe for B3D).


I appreciate the performance improvements, but I really think that a potential optimisation is not a justification for such a language limitation.

Ideally, the compiler should see whether or not the step expression is constant or variable, and only add the additional code if it is variable. As long as the behaviour is well-documented, it shouldn't be a problem.

I believe such optimisations are the reason why case statements in some languages must be constant. I think that the compiler should look at the whole switch / select construct and if all of the case expressions are constant, it will produce specially optimised code, otherwise it will generate the equivilent of a series of if...elseif...endif statements.


dmoc(Posted 2005) [#7]
<<< I agree with him