RomanYankovsky / DelphiAST

Abstract syntax tree builder for Delphi
Mozilla Public License 2.0
271 stars 116 forks source link

Changing the for-in loop variable expectation #304

Open kjpowerworld opened 4 years ago

kjpowerworld commented 4 years ago

Changing from QualifiedIdentifier to Variable allows the for-in loop to have this syntax:

for TSomeType(VarName) in Collection do

So then the VarName can be of a different type than the Collection item type and be typecast to match the item type. The above syntax compiles in Delphi.

RomanYankovsky commented 4 years ago

@kjpowerworld I can't confirm it compiles. I'm getting an error in Delphi 10.3.3:

var
  S: TList<TObject>;
  A: TForm;
begin
  for TObject(A) in S do

end;

[dcc32 Error] Unit7.pas(30): E1019 For loop control variable must be simple local variable

jkour commented 4 years ago

it should be:

for var item:TObject in S do

KyleJ61782 commented 4 years ago

@RomanYankovsky You know what, I realized what I'm seeing...I think it's something goofy with list enumerators over lists of pointers combined with typecasting that does nothing. So if I take your example code and change it to this:

var
  S: TObjectList;
  A: TForm;
begin
  for TForm(A) in S do

end;

it compiles. Obviously in this case the TForm() typecast doesn't achieve anything, but it is syntax that does compile.