[fpc-devel] Warning message for constructing an abstract class

Sven Barth pascaldragon at googlemail.com
Tue Jul 1 22:50:01 CEST 2014

Hello together!

In revision 28127 I've commited the addition of a warning message when 
constructing an abstract class (declared as "class abstract(...) ... 
end"). Previously the compiler did not acknowledge that flag in any way 
(except for the JVM target) and so I thought it was time to add an 
approbiate message. This message is however not enabled by default, but 
you can do that by either doing a

{$warn 4122 on}


{$warn 4122 error}

depending on the severity you want the message to have.

Note 1: the warning needs to be active at the location where the class 
is instantiated, not where it is declared! So better declare the 
directive inside a global include file or so... (AFAIK we can't 
currently influence that from the command line)

Note 2: the warning will only be triggered if you call a constructor on 
the typename of the abstract class, but not if call a constructor of a 
class variable of that type. That's because in the latter case the 
compiler can not know the type contained in the class variable at 
compile time (aside from special cases which are not handled either).


=== source begin ===

   TTest = class abstract
   TTestClass = class of TTest;

   o: TObject;
   c: TTestClass;
   o := TTest.Create; // warning
   o := c.Create; // no warning

=== source end ===


More information about the fpc-devel mailing list