Recent

Author Topic: syntax suggestion: foreach  (Read 6866 times)

jack616

  • Sr. Member
  • ****
  • Posts: 268
syntax suggestion: foreach
« on: May 25, 2016, 04:50:25 pm »
Using PHP a lot I often find myself typing the foreach syntax when I drop back into lazarus/fpc
I know there are a number of foreach methods but they seem to be quite specific and I can't see
a more generic one.
I don't know if this is something I've missed (again ! ... sorry folks) but it would be really nice
to have a general foreach loop similar to the PHP one that does something like:

foreach TChecklistbox.items where Tchecklistbox.items[var].checked  = true  do
begin
   whatever ....
end;

I think you can see the idea from that.
I just find that syntax very natural for many tasks.

 



Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: syntax suggestion: foreach
« Reply #1 on: May 25, 2016, 05:13:51 pm »
FPC have his own way:

Code: Pascal  [Select][+][-]
  1.   for i:=0 to CheckListBox1.Count-1 do if CheckListBox1.Checked[i] then
  2.   begin
  3.     //whatever ...
  4.   end;
  5.  
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: syntax suggestion: foreach
« Reply #2 on: May 25, 2016, 05:35:25 pm »
I don't like repeating CheckListBox1 twice, how about this:
Code: Pascal  [Select][+][-]
  1.   with CheckListBox1 do for i:=0 to Count-1 do if Checked[i] then begin
  2.     //whatever ...
  3.   end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: syntax suggestion: foreach
« Reply #3 on: May 25, 2016, 08:18:13 pm »
Code: Pascal  [Select][+][-]
  1. for t in checklistbox1.items do
  2.   twhateverthechildis(t).checked:=true;

is possible today. t must be same time as items is declared, and in many cases that is tobject, hence the typecast

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: syntax suggestion: foreach
« Reply #4 on: May 25, 2016, 09:00:00 pm »

foreach TChecklistbox.items where Tchecklistbox.items[var].checked  = true  do
begin
   whatever ....
end;

I think you can see the idea from that.
I just find that syntax very natural for many tasks.

I guess you are not referring to ForEach, because that is covered by For ..In, but about the argument where?
Note that scripts can do things compiled languages can't ... (But you can write the whole of PHP in Pascal if you like ;) )
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: syntax suggestion: foreach
« Reply #5 on: May 25, 2016, 09:04:47 pm »
In this case, given simple clauses, a where argument should be possible though.
Specialize a type, not a var.

jack616

  • Sr. Member
  • ****
  • Posts: 268
Re: syntax suggestion: foreach
« Reply #6 on: May 27, 2016, 04:04:08 pm »
Thaddy... Yes my mistake - the subject would have been better related to the "where" clause.
I won't be rewriting PHP anytime soon but if I wanted to get involved in a long project it
would be to specify a decent OS. There isn't a single OS in existance I'd call half decent right now.
In fact some are actively criminal IMO. (win10 and android being top of the pile)



BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: syntax suggestion: foreach
« Reply #7 on: May 27, 2016, 04:24:27 pm »
Do you know what would be cool?

If fpc would perform finite calculus optimizations.

E.g. you write

Code: [Select]
for i := 1 to n do
  result += i;

and it gets optimized to

Code: [Select]
if n >= 1 then
  result += n * (n + 1) div 2;



 

TinyPortal © 2005-2018