pyscripter 689 Posted October 13, 2020 (edited) I guess everyone knows this already, but I did find this Stackoverflow answer surprising. Can you guess what is the message shown? function DoSomething(SomeBoolean: Boolean) : string; begin if SomeBoolean then Result := 'abc' end; procedure TForm1.Button1Click(Sender: TObject); var xx: string; begin xx := DoSomething(True); xx := DoSomething(False); ShowMessage(xx); end; Shouldn't the above deserve a compiler warning? Edited October 13, 2020 by pyscripter Share this post Link to post
Darian Miller 361 Posted October 14, 2020 It's a "feature" not a bug. But, yes, I would love a compiler warning. Share this post Link to post
David Heffernan 2345 Posted October 14, 2020 It all went downhill when Borland decided that function return values were actually var parameters. Which means that in the case of managed types they get initialized by the caller. A function return value really should have pass by value callee to caller semantics. Share this post Link to post
Der schöne Günther 316 Posted October 14, 2020 (edited) I don't have the source at hand, but Embarcadero said that is is "too difficult" to fix, because the initialization of that variable takes place in another compiler code part that actually doesn't know how the variable is used. There is, however, absolutely no excuse that there is no compiler warning. Embarcadero itself stumbles upon these uninitialized return values. Here's an example: [RSP-18300] TJson.Format() outputs invalid JSON - Embarcadero Technologies Edited October 14, 2020 by Der schöne Günther Share this post Link to post
Stefan Glienke 2002 Posted October 14, 2020 Use Fixinsight. It catches those. Share this post Link to post
A.M. Hoornweg 144 Posted November 13, 2020 On 10/14/2020 at 8:01 AM, Stefan Glienke said: Use Fixinsight. It catches those. Thanks for mentioning this, downloading now... Share this post Link to post