Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-07

Warning message for constructing an abstract class

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sven Barth

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}

or

{$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).

E.g.

=== source begin ===

type
   TTest = class abstract
   end;
   TTestClass = class of TTest;
var
   o: TObject;
   c: TTestClass;
begin
   o := TTest.Create; // warning
   o := c.Create; // no warning
end.

=== source end ===

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote 16 lines
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} or {$warn 4122 error} depending on the severity you want the message to have.

After a little discussion we decided to enable the warning by default, so {$warn 4122 on} is not necessary. Additionally I have hooked up the message to the "constructing_abstract" identifier which also controlled the message if a class with an abstract method is instantiated. So you can now control both messages at once by using {$warn constructing_abstract error}.

Regards,
Sven

Reply by email