Sign in Join

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

Mailing lists / fpc-pascal / 2012-07

Initialization of class field

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

Unknown author
to Krzysztof
Krzysztof wrote 14 lines
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.

Michael.

Sven Barth
to Unknown author
Show quoted text 22 lines
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.

Regards,
Sven

Sven Barth
to leledumbo
leledumbo wrote
I think the fast point from OP point of view is the less typing, less source code. Not from executable point of view.

Might be, but as there are people that are complaining about class instance initialization speed (the instance's memory is initialized using FillChar) I wanted to mention this.

Regards,
Sven

Bart
to Marco van de Voort
Marco van de Voort wrote 7 lines
Hmmm, makes me wonder when typing became the limiting factor in programming :-) _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

[OT]
Ideal programming Language (ObjectPascal 10.1?), reduced typing.

Program AnyProgram;
begin
  MyPrgram := TMyProgram.Create(ReadMyMindAndCreateWhatIWantNow);
end.

Write Once, Build Different Things Any Time, Any OS, Any Platform...

Bart

Marco van de Voort
to Bart

In our previous episode, Bart said:

Show quoted text 8 lines
[OT] Ideal programming Language (ObjectPascal 10.1?), reduced typing. Program AnyProgram; begin MyPrgram := TMyProgram.Create(ReadMyMindAndCreateWhatIWantNow); end.

Actually, from experience, that isn't even enough. What you want at the beginning is often irrelevant. What you want is the endresult of after you went through the whole development trajectory :-)