views:

498

answers:

2

Hello,

I've read that Delphi was supposed to get a for each loop in Delphi 9. Did this functionality ever make it into the language? My Delphi 2009 IDE doesn't seem to recognize the for each syntax. Here's my code:

  procedure ProcessDirectory(p_Directory, p_Output : string);
  var
    files : TStringList;
    filePath : string;
  begin
    files := GetSubfiles(p_Directory);
    try
      for (filePath in files.Strings) do
      begin
        // do something
      end;

    finally
      files.Free;
    end;
  end;
+12  A: 

Yes.

But it is for..in

Try

var
  s: string;
  c: char;

begin
  s:=' Delphi Rocks!';
  for c in s do  //<--- here is the interesting part
  begin
    Application.MainForm.Caption:=Application.MainForm.Caption+c;
    Sleep(400); //delay a little to see how it works
  end;
I thought they opted for for .. in instead of for .. each in order to not add another reserved keyword. In is already a reserved keyword.
Ritsaert Hornstra
Plausible, but more likely is that the "each" in "for each" would not replace "in" but be a redundant addition ("for each .. in", vs "for .. in"). So not just a new keyword, but an entirely unnecessary one.
Deltics
+14  A: 
procedure ProcessDirectory(p_Directory, p_Output : string); 
var 
  files : TStringList; 
  filePath : string; 
begin 
  files := GetSubfiles(p_Directory); 
  try 
    for filePath in files do 
    begin 
      // do something 
    end; 

  finally 
    files.Free; 
  end; 
end; 
da-soft