[fpc-pascal] Initialization of class field

Sven Barth pascaldragon at googlemail.com
Tue Jul 31 16:56:06 CEST 2012

On 31.07.2012 14:56, michael.vancanneyt at wisa.be wrote:
> On Tue, 31 Jul 2012, Krzysztof wrote:
>> Hi,
>> I'm reading about variable initialization
>> (http://www.freepascal.org/docs-html/ref/refse22.html) . Can I
>> initialize class field too? Like in Java:
>> type
>>  TMyClass = class
>>  private
>>    FSomeField: Integer = 123;
>> end;
>> I always initialize this in overrided constructor, but above example
>> is much faster.
> Object Pascal currently does not support this, except maybe for static
> class
> variables. The compiler people should confirm this.

It doesn't work in any case currently. You can post a feature request on 
the bug tracker, but there is no promise that it will be implemented.

Regarding "fastness": Depending on how this feature would be implemented 
it can be even slower than just setting the fields by hand in the 
constructor: As the initializations need to be done by assigments 
nevertheless they can either be done in an extra (hidden) method which 
is called from all constructors of that class (leads to next topic: 
inheritance) or the code to initialize the fields can be added to all 
constructors; another possiblitiy would be to let NewInstance call the 
compiler generated virtual "InitFields" method... in two of the three 
possibilities one or more method calls (maybe even virtual) are involved.


More information about the fpc-pascal mailing list