Compiler always call class constructors even if classes are not used
Original Reporter info from Mantis: kluug.net @onpok
-
Reporter name: Ondrej Pokorny
Original Reporter info from Mantis: kluug.net @onpok
- Reporter name: Ondrej Pokorny
Description:
Usage of the "Self" class in a class constructor has to be ignored by the smart linking mechanism so that classes that are not used anywhere else are not linked into the executable.
http://docwiki.embarcadero.com/RADStudio/Rio/en/Methods_(Delphi)#Class_Constructors
This is also logical - that is exactly the difference between the initialization section and class constructors. If this difference is not present, the biggest advantage/difference compared to the initialization section of class constructors is lost.
---
I know this Delphi incompatiblity is documented: https://www.freepascal.org/docs-html/current/ref/refsu29.html#x80-1020006.5.5 but anyway IMO it cannot be really justified:
1.) If you always want to add the class to the EXE, use the class in the initialization section.
2.) If you need to call initialization code and you want to skip the class from the EXE if it isn't used elsewhere, you can use class constructors in Delphi. In FPC you don't have this possibility.
Steps to reproduce:
The following program writes "bang" when compiled with FPC but does not write anything in Delphi.
program ClassConstructors;
{$mode delphi}
type
TMyClass = class(TObject)
public
class constructor Create;
end;
{ TMyClass }
class constructor TMyClass.Create;
begin
TMyClass.Create.Free;
Writeln('bang');
end;
begin
end.
Mantis conversion info:
- Mantis ID: 36623
- Version: 3.3.1
- Monitored by: » Thaddy (Thaddy de Koning)