Mattias Gaertner wrote:
> Al Boldi <[EMAIL PROTECTED]> wrote:
> > Flavio Etrusco wrote:
> > > On 6/3/06, Al Boldi wrote:
> > >> In the fpc-rtl there is something like this:
> > >>
> > >> procedure TObject.Free;
> > >> begin
> > >>   if self<>nil then self.destroy;
> > >> end;
> > >>
> > >> Does this make sense?  i.e. How is it possible for self to be nil?
> > >
> > > 'Self' isn't a global variable associated with the method, it's simply
> > > a parameter passed implicitly to the method.
> >
> > This would imply, that we are executing the class method instead of the
> > object method.
>
> No. The 'Self' of a class method is the class.
> The Self=nil test is useful for this case:
>
> var o: TObject;
>
>   o:=TObject.Create;
>   ...
>   o.Free;
>   o:=nil;
>   ...
>   o.Free;
>
> The o is given to the method as hidden parameter, accessible via Self.

Only the constructor should be possible to be called w/o self. All others 
should be dependent on a valid self, otherwise we get something like this:

var obj: TObject;

TObject.Free;  // this won't compile, which is correct
obj.Free;      // this will compile, which is correct,
               // and gets warned of not being initialized,
               // but the error only gets detected inside
               // the class method, which is incorrect,
               // as it should have been detected in the
               // calling routine during execution.

This problem gets really bad, when you have large class hierarchies, as the 
error may only be detected in some deep object without reference to the 
causing routine.

Michael Van Canneyt wrote:
> Al Boldi wrote:
> > Does FPC have a switch to disallow executing class/definition methods,
> > and only allow executing object/instance methods?
>
> No. What on earth would be the use of that ?

This would make it easier to detect the causing routine of a non-initialized 
self.

Thanks!

--
Al

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to