* * *

Author Topic: Ada style "for" loop counters  (Read 13888 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 560
    • Burdjia
Re: Ada style "for" loop counters
« Reply #90 on: February 15, 2016, 06:07:04 pm »
If you like ADA, use ADA. 8)

BeniBela

  • Hero Member
  • *****
  • Posts: 544
    • homepage
Re: Ada style "for" loop counters
« Reply #91 on: December 18, 2016, 01:53:21 pm »
And now my program crashed again, because there are loop-scoped variables.

I had  a function like

Code: Pascal  [Select]
  1. var i: integer;
  2. ...
  3. for i := 0 to high(anArray) do
  4.    dosomething(anArray[i], anotherArray[i]);
  5.  
  6. dosomething(somethingElse, anotherArray[i]);
  7.  

copy/pasted dosomething. The value of i seems to have changed between fpc versions.

If it was

Code: Pascal  [Select]
  1. for var i := 0 to high(anArray) do
  2.    dosomething(anArray[i], anotherArray[i]);
  3. ...
  4.  

it would have been an obvious compile error.

Now it does not even give a hint.

zeljko

  • Hero Member
  • *****
  • Posts: 888
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Ada style "for" loop counters
« Reply #92 on: December 18, 2016, 02:30:45 pm »
Never use for variable after the loop.

This is bad:
  var i: integer;
begin
  for i := 0 to high(anArray) do
    dosomething(anArray, anotherArray);
  dosomething(somethingElse, anotherArray);
end;

This is bad:
  var i,x: integer;
begin
  x := -1;
  for i := 0 to high(anArray) do
  begin
    dosomething(anArray, anotherArray);
    x := i;
  end;
  if x >= 0 then
    dosomething(somethingElse, anotherArray
  • );

end;

Thaddy

  • Hero Member
  • *****
  • Posts: 3196
Re: Ada style "for" loop counters
« Reply #93 on: December 18, 2016, 03:15:35 pm »
Now it does not even give a hint.
Why a hint?
You instructed to compiler to -- from? Why would the index  be valid after that? It is not an index anymore.

Leledumbo

  • Hero Member
  • *****
  • Posts: 7528
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Ada style "for" loop counters
« Reply #94 on: December 18, 2016, 03:26:52 pm »
And now my program crashed again, because there are loop-scoped variables.

I had  a function like
...
copy/pasted dosomething. The value of i seems to have changed between fpc versions.
Page 57:
"After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined."
It was and is designed that way, it never changes. It's also never a problem unless you think that all repetitive statements are equal, i.e. transformable from one to another within the same type value boundary, because they are not in Pascal world.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5247
Re: Ada style "for" loop counters
« Reply #95 on: December 18, 2016, 03:57:10 pm »
Note that the compiler "could" see that a variable is read that is not defined at that point and emit a warning.

The compiler is not perfect and certainly could have some polishing here and there. But not every little flaw means that the syntax should be blamed and radically changed.

BeniBela

  • Hero Member
  • *****
  • Posts: 544
    • homepage
Re: Ada style "for" loop counters
« Reply #96 on: December 18, 2016, 06:22:35 pm »
Why would the index  be valid after that? It is not an index anymore.

That is why fpc should not compile it happily

Note that the compiler "could"should see that a variable is read that is not defined at that point and emit a warning.

 

ftfy

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5247
Re: Ada style "for" loop counters
« Reply #97 on: December 18, 2016, 06:50:06 pm »
Note that the compiler "could"should see that a variable is read that is not defined at that point and emit a warning.
ftfy

Well, everybody says that about any problem they have. But if you want to hurry up, start creating a patch.

BeniBela

  • Hero Member
  • *****
  • Posts: 544
    • homepage
Re: Ada style "for" loop counters
« Reply #98 on: December 19, 2016, 08:33:01 pm »
When there are ada-like patches they are not merged anyways (like the ifthen intrinsic debacle)...

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5247
Re: Ada style "for" loop counters
« Reply #99 on: December 19, 2016, 10:32:19 pm »
When there are ada-like patches they are not merged anyways (like the ifthen intrinsic debacle)...

I meant a patch to add the warning, not the Ada syntax (which I think is pointless to introduce that now, it will only cause more confusion and dialectitis)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus