chkaufmann 17 Posted July 30, 2020 Hi, I just fixed an error in the following function. I had to add the last line where I assign tmp to Result. Now I wonder, why I didn't get a compiler warning like "return value of function might be undefined.". I get such errors in the same project with other methods, so the options are set correctly. Christian class function TLnxResult.GenForEvent(AData: TLnxObject; AEventId, AAgegroupId: Integer): IBSEnumerable<IBSSwLenexResult>; var data : TLnxAbstractObject; clubs : TLnxCollection; tmp : IBSList<IBSSwLenexResult>; begin tmp := TBSGenerics.GenList<IBSSwLenexResult>; data := AData; while Assigned(data) and (data.Tag <> lxCOMPETITION) do data := data.Parent; clubs := TLnxCollection(TLnxObject(data).ChildObject[lxCLUBS]); if Assigned(clubs) then for var ixC := 0 to clubs.ChildCount - 1 do begin for var e in TLnxEntryResult.GenForClub(lxRESULTS, TLnxCollection(clubs.Objects[ixC].ChildObject[lxPERSONS]), AEventId, AAgegroupId) do tmp.Add(e as IBSSwLenexResult); for var e in TLnxEntryResult.GenForClub(lxRESULTS, TLnxCollection(clubs.Objects[ixC].ChildObject[lxRELAYS]), AEventId, AAgegroupId) do tmp.Add(e as IBSSwLenexResult); end; tmp.Sort(function(const AResult1, AResult2: IBSSwLenexResult): Integer begin Result := AResult1.Place(nil) - AResult2.Place(nil); if Result.IsZero and (AResult1.Place(nil) = BSSW_MAX_PLACE) then Result := AResult1.Status.AsInteger - AResult2.Status.AsInteger; end); Result := tmp; // <<<< this line was missing. end; Share this post Link to post
David Heffernan 2345 Posted July 30, 2020 You don't get this because the return type is a managed type and so is actually passed as an extra var parameter. So the variable is assumed to have been default initialised outside the function. I'm not trying to defend the compiler design here. The handling of function return variables is a complete mess in my view. They should be passed by value from callee to caller. The whole hidden var param is a weird hack from the ancient past that should never have been done. 2 Share this post Link to post
chkaufmann 17 Posted July 30, 2020 So just that I get it right: Because my return value is an interface type, the compiler adds something like this as first statement of the method: Result := nil; Chrisitan Share this post Link to post
David Heffernan 2345 Posted July 30, 2020 9 minutes ago, chkaufmann said: So just that I get it right: Because my return value is an interface type, the compiler adds something like this as first statement of the method: Result := nil; Not quite. The default initialisation is outside the function. Share this post Link to post
Arnaud Bouchez 407 Posted July 30, 2020 (edited) 54 minutes ago, David Heffernan said: You don't get this because the return type is a managed type and so is actually passed as an extra var parameter. Indeed. I would have written an "out" parameter in the docs, at least. The function should set or initialize the value. And the compiler should emit a warning in your case. FPC is much more paranoid about such warnings, but at least it tries to warn anything dubious, and allow to disable false positives with the {$H-} trick on the faulty line. Edited July 30, 2020 by Arnaud Bouchez 1 Share this post Link to post
Stefan Glienke 2002 Posted July 30, 2020 Reported and complained about numerous times - like https://quality.embarcadero.com/browse/RSP-21023 I think Allen Bauer somewhere explained this a bit more detailed but here is the short version I remember: The compiler internally takes some shortcuts and the part that produces the warning does not know that the var parameter it sees was originally meant to be the function result. My advice: spend a few bucks for FixInsight - it will catch this and other things the compiler does not. Share this post Link to post
luebbe 26 Posted July 30, 2020 (edited) Yes, FixInsight is great. Although it doesn't get as many updates anymore as it did in the first year. Basically only one per Delphi version. But 2020 seems to be a better year for FixInsight customers 😉 Edited July 30, 2020 by luebbe Share this post Link to post
David Heffernan 2345 Posted July 30, 2020 17 minutes ago, luebbe said: Although it doesn't get as many updates anymore as it did in the first year. Probably it is mature now. Share this post Link to post
luebbe 26 Posted July 30, 2020 13 minutes ago, David Heffernan said: Probably it is mature now. Yes and no. There was almost no activity on FixInsight from the end of 2017 until the end of 2019 and there were several open issues. By the end of 2019 bugs were fixed again and new features were added. Share this post Link to post
Anders Melander 1782 Posted July 30, 2020 4 hours ago, Stefan Glienke said: Reported and complained about numerous times - like https://quality.embarcadero.com/browse/RSP-21023 "Works As Expected" - What The Hell? Stupidity like that is high among the reasons why I no longer bother reporting bugs. Share this post Link to post