Forum > FPC development

Why can't assign to "for..IN" variable?

(1/5) > >>

kupferstecher:
The below code doesn't compile. The error message I understand, but I don't see why this shouldn't work. The compiler would have to make an extra step, i.e. after fetching the value from the array and doing the loop calculations writing the result back to the array. The compiler already detects that the variable is modified, so writing it back in the end of the loop should be no problem.

Is there an explanation why this shouldn't work?

The iterations aren't changed by such an action. In opposite in a normal for..to loop this would be the case.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;var  scalar: double;  vector: array of double;begin    for scalar in vector do scalar:= 0; //Error: Illegal assignment to for-loop variable "scalar" end.

marcov:
The for..in is based on an interface that returns a value, not a reference. This for iterations over collections where the results are calculated.

Delphi doesn't support it either.

kupferstecher:
Perhaps in the type IEnumerator there could be an additional method like ReturnCurrent.

Also iterations over arrays could support it independently to collections.
But I see, this would quickly get complicated.

marcov:
I don't know how complicated it would be either.

But while I usually think most proposed extensions are mindless bulletlist hunting and syntax grafiti, I think this one would actually be useful. (depending on the work of course).

Phemtik:
in Ada 2012, for example, this feature was added. So it is quite new there.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for x of b loop    x := -x; -- access and modify each elementend loop;
Would be a nice feature to have this in Free Pascal also, but I don't know if it's difficult or good to change the behaviour of an existing feature.
It would make code cleaner on some occasions.

Navigation

[0] Message Index

[#] Next page

Go to full version