Kryvich 165 Posted November 4, 2018 I like to write code without warnings and hints issued by the compiler. Today I encountered the following warning: program TestCaseW1010; {$APPTYPE CONSOLE} type TPerson = class Name: string; function ToString(Quote: boolean): string; overload; end; function TPerson.ToString(Quote: boolean): string; begin if Quote then Result := '"' + Name + '"' else Result := Name end; var Person: TPerson; begin Person := TPerson.Create; Person.Name := 'John'; Writeln(Person.ToString); // --> TPerson Writeln(Person.ToString(False)); // --> John Writeln(Person.ToString(True)); // --> "John" Readln; Person.Free; end. The compiler shows W1010 Method 'ToString' hides virtual method of base type 'TObject'. In fact, he does not. Is it a false positive warning, or is it something wrong with my code? Share this post Link to post
Uwe Raabe 2052 Posted November 4, 2018 The correct declaration would be: type TPerson = class Name: string; function ToString(Quote: boolean): string; reintroduce; overload; end; 2 1 Share this post Link to post
Kryvich 165 Posted November 4, 2018 (edited) Ouch! Thank you. :) P.S. I encountered this situation in GOLD Parser Engine 5 that I've converted from Lazarus. Now this project compiles without any warnings, I'm happy! Edited November 4, 2018 by Kryvich 1 Share this post Link to post
Stefan Glienke 1996 Posted November 5, 2018 That fact that you have to mark overloads to a virtual method as reintroduce always bothers me but I never cba to actually report it. Share this post Link to post
Hallvard Vassbotn 3 Posted November 6, 2018 I think the requirement to explicitly state reintroduce is a good thing. It avoids the issue of forgetting to add the override directive - if you really intended to do so. Share this post Link to post
Sherlock 662 Posted November 7, 2018 From a pure linguistic point of view: Where does the need for reintroducing a purely virtual aka non existent thing come from? There seems to be a need for this that is deep in the compiler. Would be a nice research project, or something the experts can explain. Share this post Link to post
David Heffernan 2345 Posted November 7, 2018 Why don't you override the method from TObject? 1 Share this post Link to post
Zacherl 3 Posted November 7, 2018 (edited) 43 minutes ago, David Heffernan said: Why don't you override the method from TObject? That's exactly the problem and as well the reason for the warning. The OP does not actually override TObject.ToString but introduces a new method (because of the different signature). The override just does nothing. Edited November 7, 2018 by Zacherl Share this post Link to post
David Heffernan 2345 Posted November 8, 2018 22 hours ago, Zacherl said: That's exactly the problem and as well the reason for the warning. The OP does not actually override TObject.ToString but introduces a new method (because of the different signature). The override just does nothing. It's not that the override does nothing. There is no override. It makes no sense to me to reintroduce a method of that name. 1 Share this post Link to post