Question

This is probably similar / continuation on the previous question below:

Why Delphi XE3 gives "E2382 Cannot call constructors using instance variables"?

Now I'm trying Delphi XE4 with the same code (with 'constructor' changed to 'procedure' as per the solution of the above question).

Now I have also these things in a generics list, i.e. I have

TCoordRect = object
public
  function Something: Boolean;
end;

and then a list of these in a function parameter, which I loop through and try to access the items directly:

function DoSomething(AList: TList<TCoordRect>): Boolean;
var
  i: Integer;
begin
  Result := False;
  for i := 0 to AList.Count - 1 do
  begin
    Result := Result or AList[i].Something;  // <-- Here comes the compiler error!
  end;
end;

This gives the compiler error "E2036 Variable required". However, if I don't access it directly, i.e put instead a local variable and use that first, then it works:

function DoSomething(AList: TList<TCoordRect>): Boolean;
var
  i: Integer;
  ListItem: TCoordRect;
begin
  Result := False;
  for i := 0 to AList.Count - 1 do
  begin
    ListItem := AList[i];
    Result := Result or ListItem.Something;  // <-- Now this compiles!
  end;
end;

And another "workaround" is to remove all these 'object' types and change them to 'class', but I'm curious as to why this does not work like it used to? Is it again just something with "the compiler moving towards mobile development" or is there some more specific reason, or is this even a bug? BTW I also reported this as a QC issue, so will see if something comes from there.

Was it helpful?

Solution

It's a compiler bug, and it's present in all earlier versions of the compiler. The fault is not limited to XE4. Submitting a QC report is the correct response.

I would not be surprised if Embarcadero never attempt to fix it. That's because you are using deprecated object. Switch to using record and the code compiles.

The issue you have uncovered in this question is unrelated to the SO question you refer to at the top of your question.

Incidentally, this really is a case of old meets new. Legacy Turbo Pascal objects, and modern day generic containers. You are mixing oil and water!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top