[fpc-devel] type discussion

Michael Van Canneyt michael at freepascal.org
Thu Jun 2 16:51:52 CEST 2005


>
> var strlist : TStringlist;
> strlist := Tstringlist.create;
>
> I know strlist is a Tstringlist, the compiler knows it too as I have declared 
> it so why do I have to spell it out in the creation process?

For 2 reasons:

First, with

  strlist.create;

It is not clear whether strlist is already initialized or not;
there is no way it can now this.

  strlist := Tstringlist.create;

makes this explicitly clear.

Second, with

  strlist.create;

It is not clear from reading whether create is a constructor or not.
Create() is just a convention, which you can decide to ignore.
Here again, with

  strlist := TStringlist.create;

Create MUST be a constructor, since you are initializing the variable.

Look at other languages: VB, PHP, Perl, Python.
They all require the use of the 'new' (or a variant thereof)  keyword
when the constructor must be called:

   strlist = new tstringlist;

Michael.




More information about the fpc-devel mailing list