dummzeuch 1505 Posted June 29, 2022 I just for the first time ever came across the warning "[DCC Warning]: W1013 Constant 0 converted to NIL". I got it for the following piece of code: var SomeList: TList; //... SomeObj := SomeList.Extract(0); // <== here WTF? I would have expected a compile error, not a warning because TList.Extract is declared as: function Extract(Item: Pointer): Pointer; inline; without any overload. I overlooked that warning a first, expecting an overload with an integer parameter (the index into the list like with Delete), which would make a lot of sense. (This is with Delphi XE2.) So apparently the compiler automatically converts the integer 0 to a nil pointer. Who (tf) considered this a feature? This is Pascal, after all, not C and is supposed to be type safe! Share this post Link to post
Lajos Juhász 293 Posted June 29, 2022 (edited) The exact same is with Delphi 11. Tested with: procedure TForm1.FormCreate(Sender: TObject); var f: pointer; begin f:=0; end; [dcc32 Warning] Unit1.pas(29): W1013 Constant 0 converted to NIL Here is what helps says about this warning: The Delphi compiler now allows the constant 0 to be used in pointer expressions in place of NIL. This change was made to allow older code to still compile with changes which were made in the low-level RTL. program Produce; procedure p0(p : Pointer); begin end; begin p0(0); end. In this example, the procedure p0 is declared to take a Pointer parameter yet the constant 0 is passed. The compiler will perform the necessary conversions internally, changing 0 into NIL, so that the code will function properly. program Solve; procedure p0(p : Pointer); begin end; begin p0(NIL); end. There are two approaches to solving this problem. In the case above the constant 0 has been replaced with NIL. Alternatively the procedure definition could be changed so that the parameter type is of Integer type. Edited June 29, 2022 by Lajos Juhász Share this post Link to post
Vandrovnik 214 Posted June 29, 2022 It is a feature, not a bug 🙂 (I have never seen it before.) https://docwiki.embarcadero.com/RADStudio/Sydney/en/W1013_Constant_0_converted_to_NIL_(Delphi) Share this post Link to post
Mike Torrettinni 198 Posted June 29, 2022 I noticed this a few days ago, when by mistake assigned 0 to array: var arr: array of integer; begin arr := 0; end. It gives the same W1013 warning. Share this post Link to post
Attila Kovacs 629 Posted June 29, 2022 (edited) 29 minutes ago, dummzeuch said: is supposed to be type safe! Well, it is. Explicit. Edited June 29, 2022 by Attila Kovacs Share this post Link to post