Lazarus

Miscellaneous => Suggestions => Topic started by: jack616 on May 25, 2016, 04:50:25 pm

Title: syntax suggestion: foreach
Post by: jack616 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.

 


Title: Re: syntax suggestion: foreach
Post by: Edson 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.  
Title: Re: syntax suggestion: foreach
Post by: engkin 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;
Title: Re: syntax suggestion: foreach
Post by: marcov 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
Title: Re: syntax suggestion: foreach
Post by: Thaddy 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 ;) )
Title: Re: syntax suggestion: foreach
Post by: Thaddy on May 25, 2016, 09:04:47 pm
In this case, given simple clauses, a where argument should be possible though.
Title: Re: syntax suggestion: foreach
Post by: jack616 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)


Title: Re: syntax suggestion: foreach
Post by: BeniBela on May 27, 2016, 04:24:27 pm
Do you know what would be cool?

If fpc would perform finite calculus optimizations (http://stackoverflow.com/questions/37479552/convert-conditional-summation-to-closed-form-solution).

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