Снова общался с Бенджамином Ковачем, распросил про реализацию цикла FOR в Modula-2 Rev. 2010, есть ли отличия от обероновского FOR. И вот что поведал Бенджамин.
<trijezdci> the for loop in r10 is a foreach loop
<trijezdci> there is no, for x := start to stop do
<trijezdci> and the loop control variable is declared by the loop header and only visible inside the loop's body, but immutable inside the body
<trijezdci> FOR foo IN bar DO
<trijezdci> (* foo is visible here, but immutable here *)
<trijezdci> (* foo is no longer visible here *)
<trijezdci> basically this is like Ada
<trijezdci> for reverse order the syntax is FOR DESCENDING foo IN bar DO
<drrob1> So how would I code FOR i := 1 TO 10 DO ?
<trijezdci> FOR i IN [1..10] OF INTEGER DO
<ttmrichter> Oh, ranges are a data type now?
<drrob1> The brackets are set notation, right?
<trijezdci> [1..10] OF INTEGER <= range
<trijezdci> ranges are always typed
<trijezdci> no, square brackets in this notation indicate a range
<drrob1> Is that new?
<trijezdci> no, you have this already in Pascal
<Zorko> immutable = unchangeable
<trijezdci> read only, yes
<Zorko> then we cannot pass this var as VAR parameter into procedure?
<trijezdci> the loop header will of course update the control variable on each loop pass, but inside the body of the loop it is treated like a constant
<Zorko> it's very good that you told
<trijezdci> yes, you cannot pass the control variable of a loop as a VAR parameter
<trijezdci> only as a value parameter or a CONST parameter
<trijezdci> CONST parameters are like VAR, but they enforce immutability in the function passed to
<trijezdci> for math iterations like Sum over all n, where n from 0 to 10 for 2n+1 you simply iterate over the n, just like it is in math
<trijezdci> FOR n IN [1..10] OF INTEGER DO
<trijezdci> sum := sum + 2 * n + 1;
<trijezdci> this looks to a programmer like there is a wasteful 2*n multiplication inside the loop
<trijezdci> but in reality this is a loop invariant that a good compiler will move outside the loop anyway
<trijezdci> so trying to avoid writing it this way would be nothing more than premature optimisation
<trijezdci> but this is so deep in the psyche of programmers used to the for to by loop that it is difficult to get them to accept this
<trijezdci> even Rick, who is a mathematician (professor for mathematics actually) had difficulties accepting this
<trijezdci> the sum := sum + 2*n + k form is exactly the way it is written in a math formula, but he had very serious trouble accepting that the code should be written like this
<trijezdci> it took him at least a year to agree that this was alright and we would remove the for-to-by loop
<Zorko> I just wanted to find out whether M2R10 has not problems with "last value of type" MAX(T) in loop with counter
<Zorko> and it seems that all is well
<trijezdci> it wont
<trijezdci> also, there is no problem with "what is the value of x after the loop has finished"
<trijezdci> because x is no longer visible outside the loop
<Zorko> yes, it's also good
<trijezdci> problem avoided
<Zorko> and a developer cannot change loop counter inside the loop
<Zorko> well, I would call it an ideal solution
<Zorko> interesting, why prof. Wirth accepts this "imperfect" FOR in Oberon-07
<trijezdci> inertia I think
Публикуется с разрешения Бенджамина Ковача, хотя он предпочёл бы публиковать не этот лог, а точную выдержку из спецификации с EBNF. Так что