Sign in Join

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

Mailing lists / fpc-pascal / 2009-04

Difference between initialization and begin in a unit

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Mattias Gaertner
to leledumbo
Show quoted text 27 lines
Might be a stupid question, but it's nowhere mentioned in the documentation though allowed. What are the differences between: unit A; interface implementation initialization <initialization code> end. and: unit B; interface implementation begin <initialization code> end.

AFAIK it is syntactic sugar. They are the same.

Mattias

Cox, Stuart TRAN:EX
to Mattias Gaertner

Could there be one slight difference in that an initialization section is required if there's going to be a finalization section in the unit? That is, there can't be a finalization section in the absence of an initialization section?

Stu Cox
Project Management Technician
Southern Interior Region
Ministry of Transportation and Infrastructure
342-447 Columbia St.,
Kamloops, BC V2C 2T3
p: 250-828-4320
f: 250-828-4229

Earlier message 43 lines
-----Original Message----- From: On Behalf Of Mattias Gaertner Sent: Thursday, April 16, 2009 1:36 PM To: Subject: Re: [fpc-pascal] Difference between initialization and begin in a unit On Sun, 12 Apr 2009 20:11:42 -0700 (PDT) leledumbo wrote: > > Might be a stupid question, but it's nowhere mentioned in the > documentation though allowed. What are the differences between: > > unit A; > > interface > > implementation > > initialization > <initialization code> > > end. > > and: > > unit B; > > interface > > implementation > > begin > <initialization code> > > end. AFAIK it is syntactic sugar. They are the same. Mattias
Benedikt Schindler
to leledumbo: Might be a stupid question, but it's nowhere mentioned in the documentation though…

Unit B should just work if it is named "Programm B" In a Unit there didn't just exist a begin. (as far as i know my programms :) )

The initialization section is called when the unit is linked the first time into the programm by a "uses" class.
so it is called before everithing else is going on, even before the normal programm ever did a single step.

The initialization section is good for Units that you write, without knowing the programm where it is used in.
So you could initialize some variables or objects, and you don't need the programmer of the programm who uses your
Unit. (It is also good if you are the programmer who uses the unit. Because everything what is needed for the Unit stays in that Unit.)

------------------------------------------------------------

unit A;
interface
type Tsomething = class(TObject)
   ...
 public
   procedure Hello;
  ....
 end;
var
something : TSomething;
ProgrammDir : string;
implementation

....

initialization
  something := Tsomething.create();
  ProgrammDir := Application.Params[0];
finalization
  something.free;
end.
------------------------------------------------------------

no everyone who uses the Unit A could just use "something.hello" without creating the object. (because the object just exists from the beginning.)

Beni

btw: i shouldn't post when i am drunk :)

Show quoted text 27 lines
Might be a stupid question, but it's nowhere mentioned in the documentation though allowed. What are the differences between: unit A; interface implementation initialization <initialization code> end. and: unit B; interface implementation begin <initialization code> end.