Recent

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

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Ada style "for" loop counters
« Reply #45 on: January 16, 2016, 01:40:43 am »
procedure "ShowA" is in the same scope as the "a" was declared and i can't access "a"?


Obviously showA is not in the same scope as a. the first a occurs after the end of showA

Or did you mean my second posted code? There showA can access a just fine. But showA cannot be called from outside the loop

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Ada style "for" loop counters
« Reply #46 on: January 16, 2016, 02:46:50 pm »
The proposal of this post is suppress the var declaration from for control variables, to simplify or make the code more "readable"

Then this:
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.  a:byte;  //<-- this was suppress
  4.   procedure ShowA;
  5.   begin
  6.     writeln(a);
  7.   end;
  8. begin
  9.   for a:=0 to 10 do
  10.     ShowA;
  11. end;

becomes:

Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var p: array[0..10] of TProcedure;
  3. begin
  4.   for a:=0 to 10 do begin
  5.     p[a] := ( procedure ShowA;
  6.     begin
  7.       writeln(a);
  8.     end)
  9.   end;
  10.   for q in p do q();
  11. end;

Thanks benibella. you have a good point, but i really can't see a real advantage to this proposal, except make lazys programmers happy.
i remember my teacher saying how angry any new pascal student when compiler show messages like:
";" found, expected ")"
... and saying "why not just compile it and closing by himself?"

I believe compiler suppress is a bad idea in any cases;

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Ada style "for" loop counters
« Reply #47 on: January 17, 2016, 05:02:22 pm »
By the way:

Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.  a:byte;  //<-- this was suppress
  4.  

See here:
http://wiki.freepascal.org/Example:_Why_the_loop_variable_should_be_of_signed_type

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Ada style "for" loop counters
« Reply #48 on: January 17, 2016, 05:04:35 pm »
The proposal of this post is suppress the var declaration from for control variables, to simplify or make the code more "readable"

Thanks benibella. you have a good point, but i really can't see a real advantage to this proposal, except make lazys programmers happy.
i remember my teacher saying how angry any new pascal student when compiler show messages like:
";" found, expected ")"
... and saying "why not just compile it and closing by himself?"

I believe compiler suppress is a bad idea in any cases;


Repeating what I've said elsewhere.

1. It is most definitely not about making lazy programmers happy.

2. It is about simplifying the var declaration which improves
    Readability of the vars section: focus on things that are important/relevant
    Safety: the compiler will always choose the correct index type
    Maintainability: as the code evolves, there is less attention needed to making sure the index types are updated

3. As others have raised ridiculous examples to ridicule this proposal - your ridiculous example ";" remains as ridiculous as the others.

4. And finally, not only is this not inspired by laziness, it is inspired by a language that is both largely influenced by Pascal and also much more type rigid than Pascal.  Yet for the purpose of loop indexes they had a fit of rational simplicity.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ada style "for" loop counters
« Reply #49 on: January 17, 2016, 05:07:13 pm »
By the way:

See here:
http://wiki.freepascal.org/Example:_Why_the_loop_variable_should_be_of_signed_type
Yups, that's a very annoying one. Especially when converting code from c.

Unfortunately it is not always possible to use a signed type in which case you end up using while loops (or a lot of casting) :-(

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Ada style "for" loop counters
« Reply #50 on: January 17, 2016, 05:15:26 pm »

However, this should be valid code:


Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. begin
  3.   for a:=0 to 10 do begin
  4.     procedure ShowA;
  5.     begin
  6.       writeln(a);
  7.     end;
  8.     ShowA;
  9.   end;
  10. end;
  11.  

And I am not kidding

You can't declare a procedure within the executing part of a procedure.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Ada style "for" loop counters
« Reply #51 on: January 17, 2016, 05:23:43 pm »
You can't declare a procedure within the executing part of a procedure.
https://en.wikipedia.org/wiki/Anonymous_procedure
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Ada style "for" loop counters
« Reply #52 on: January 17, 2016, 05:52:05 pm »

However, this should be valid code:


Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. begin
  3.   for a:=0 to 10 do begin
  4.     procedure ShowA;
  5.     begin
  6.       writeln(a);
  7.     end;
  8.     ShowA;
  9.   end;
  10. end;
  11.  

And I am not kidding

You can't declare a procedure within the executing part of a procedure.

But I want to!



I just found a great example with a for in loop. There you  must use this automatically typed variable. You cannot loop without it:

Code: Pascal  [Select][+][-]
  1. var a: array of record
  2.   x, y: integer;
  3. end;
  4. begin
  5.   SetLength(a, 10);
  6.   for r in a do begin
  7.     writeln(r.x, ' ', r.y);
  8.   end;
  9. end
  10.  

You cannot declare r as r: record x, y: integer; end. It would be another record type. If a is from a library, it is given and  you cannot change its type.

It is impossible to use for-in without autodeclared types.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Ada style "for" loop counters
« Reply #53 on: January 17, 2016, 06:05:37 pm »

However, this should be valid code:


Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. begin
  3.   for a:=0 to 10 do begin
  4.     procedure ShowA;
  5.     begin
  6.       writeln(a);
  7.     end;
  8.     ShowA;
  9.   end;
  10. end;
  11.  

And I am not kidding

You can't declare a procedure within the executing part of a procedure.

But I want to!

What would be the advantage over this valid code?
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   a: Integer;
  4.  
  5.   procedure ShowA;
  6.   begin
  7.     writeln(a);
  8.   end;
  9.  
  10. begin
  11.   for a:=0 to 10 do begin
  12.     ShowA;
  13.   end;
  14. end;
  15.  

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Ada style "for" loop counters
« Reply #54 on: January 17, 2016, 06:53:25 pm »
I just found a great example with a for in loop. There you  must use this automatically typed variable. You cannot loop without it:

Code: Pascal  [Select][+][-]
  1. var a: array of record
  2.   x, y: integer;
  3. end;
  4. begin
  5.   SetLength(a, 10);
  6.   for r in a do begin
  7.     writeln(r.x, ' ', r.y);
  8.   end;
  9. end
  10.  

You cannot declare r as r: record x, y: integer; end. It would be another record type. If a is from a library, it is given and  you cannot change its type.

It is impossible to use for-in without autodeclared types.
If there's such a library, that's a badly written one. Seriously, ask the author to write Pascal correctly. Structured types must be defined and exported in interface section. Laziness has no support in Pascal.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Ada style "for" loop counters
« Reply #55 on: January 17, 2016, 06:55:58 pm »
If there's such a library, that's a badly written one. Seriously, ask the author to write Pascal correctly. Structured types must be defined and exported in interface section. Laziness has no support in Pascal.
+1


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Ada style "for" loop counters
« Reply #56 on: January 17, 2016, 06:57:38 pm »
I actually agree with Benibela that this is a weak point, but it can't really be helped, except by declaring a separate type.

Pascal is a strong typed language, and too much weakening of the principle will make the whole system come crashing down. And there has been quite some weakening already (like variants, though for a more lofty goal than saving on typing)

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Ada style "for" loop counters
« Reply #57 on: January 17, 2016, 07:03:27 pm »
I actually agree with Benibela that this is a weak point, but it can't really be helped, except by declaring a separate type.

Pascal is a strong typed language, and too much weakening of the principle will make the whole system come crashing down. And there has been quite some weakening already (like variants, though for a more lofty goal than saving on typing)

Yes, but type inference still means strongly typed, or doesn't it? But if the compiler infers, programmers tend to create bugs.
Specialize a type, not a var.

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Ada style "for" loop counters
« Reply #58 on: January 17, 2016, 07:49:23 pm »

If there's such a library, that's a badly written one. Seriously, ask the author to write Pascal correctly. Structured types must be defined and exported in interface section. Laziness has no support in Pascal.

I wrote that library!

I think it is a great way to handle internal types that have to be there, but the library user should be discouraged to create new instances of them. Exporting them would be the worst.


Pascal is a strong typed language

And ADA is supposed to be an even strongerly typed language!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Ada style "for" loop counters
« Reply #59 on: January 17, 2016, 08:08:14 pm »

If there's such a library, that's a badly written one. Seriously, ask the author to write Pascal correctly. Structured types must be defined and exported in interface section. Laziness has no support in Pascal.

I wrote that library!

I think it is a great way to handle internal types that have to be there, but the library user should be discouraged to create new instances of them. Exporting them would be the worst.

then do it correctly no half a$$ solutions
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. type
  3.   TInternal = record
  4.     x, y: integer;
  5.   end;
  6. var
  7.   a: array of Tinternal;
  8.   r: TInternal;
  9. begin
  10.   SetLength(a, 10);
  11.   for r in a do begin
  12.     writeln(r.x, ' ', r.y);
  13.   end;
  14. end;
  15.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018