Why is there 'loopn.i++'?
as much as advanced the "auto loop" is, it still needs "
a hamster to spin the wheel" (what? )
and you end up needing "
something" that increments the counter
Note, i++ is a
saturated increment operator(2), which guarantees the counter never goes out of its range
having to increment the loop counter makes a mockery of it since you could make it any value: 1, 2, 24, -6, 3,...
the symbol ' means "property", you assign the range { lower .. upper } property to the loop counter
always guaranteed: lower <= counter <= upper property is a great trick in my-c as it permits other tricks
This example uses a side effect to automatically re-initialize the counter when it encounters the keyword "during", and that's it, all the rest works exactly as the previous two examples, just it's all "hidden", but as soon as the counter is equal to the counter'range.upper, is_EOL becomes true, is_done becomes also true, all auto-magically, and the loop ends.
However, in this example there is also a second side effect, and a difference a difference from the previous first example: this time, if the loop ends without an error then the counter is ***exactly*** equal to the upper bound.
C/89 loop:for (i=0; i<10; i++) { } ;
/* i'range = { 0 .. 9 }
the counter i is equal to 10 at the end of the loop ----------> potentially an "out by one" bug(1)
C/99 loop:for (uint32_t i=0; i<10; i++) { } ;
/* i'range = { 0 .. 9 }
the counter i is ... no more existing after the loop
my c previous first example:the counter is equal to 10 at the end of the loop ------------> potentially an "out by one" bug(1)
my C auto loop:loop.i'range = { 0 .. 9 };
during loop { loop.i++; }
/* i'range = { 0 .. 9 }
loop.i = 9 at the end of the loop
Given that context, the C style of having the loop counter within the loop definition seems preferable: it's in one defined place, easily digested and no hidden gotchas buried in the loop somewhere (at least, there shouldn't be).
in C you can go over the counter range and you can be "out of one"; in my-c it is impossible as being
strict in-range is guaranteed.
In C you have to think a way to "exit" from an inner loop (goto? break?), in my-c you just need to invoke .is_EOL or .is_done, that's good as it facilitates step-by-step manual debugging and ICE-testcases.
edit:
(1) when the counter is used in some following statement under the belief that it is in-range
(2) you can override its reactive code callback and invoke "panic()" or a "console_out_warning()" to be warned that a counter has gone out of its range -> probably a bug.