Answer Alex is the correct solution to the problem. And it is also good to return from a function as soon as the answer is known.
I would like to talk more about this answer. In particular, I would like to answer the question that you asked in the comments on Alex:
As an aside ... why not the original work? T is derived from X.
The problem code is here:
function XList<T>.Find(Id: Integer): T; var t: X; begin for t in Self do if t.Id = Id then Result := t; end;
A way to think about generics is to imagine what the code looks like when you instantiate a type and set a specific type parameter. In this case, replace T with Y Then the code looks like this:
function XList_Y.Find(Id: Integer): Y; var t: X; begin for t in Self do if t.Id = Id then Result := t; end;
Now you have a problem in the line that assigns Result :
Result := t;
Well, Result is of type Y , but T is of type X The relationship between X and Y is that Y derived from X So the instance of Y is X But instance X not Y And so the appointment is not valid.
As Alex pointed out correctly, you need to declare a loop variable of type T Personally, I would write the code as follows:
function XList<T>.Find(Id: Integer): T; begin for Result in Self do if Result.Id = Id then exit; Result := nil;
This also applies to the problem that your search procedure does not return a return value if the item is not found. This issue you were compiling about would warn that you received the code before the actual compilation. I hope you turn on compiler warnings and work with them when they appear!
David heffernan
source share