[fpc-pascal] Implementing a true Singleton - Can we decrease thevisibility of a method?

Matt Emson memsom at interalpha.co.uk
Fri Dec 8 14:30:54 CET 2006


> On 8 dec 2006, at 10:55, Graeme Geldenhuys wrote:
>
> >  I still don't know why we can't decrease visibility in Free Pascal.
> > Is there some internal language design that prevents it?
>
> At least Borland explicitly says you cannot do that:
>    http://info.borland.com/techpubs/delphi/delphi5/oplg/classes.html

Delphi 5 onwards generates a hint for each method that is of a lower
visibility thatn its ancestors implementation.

I've used the following to implement a singleton before:

unit x;

interface

type
  ISingleton = interface [-----GUID GOES HERE-----]
    procedure SingletonAction;
  end;

var
  MySingleton: ISingleton;

implementation

type
  TSingleton = class(TInterfacedObject, ISingleton)
  private
     procedure SingletonAction;
  end;

   procedure TSingleton .SingletonAction;
   begin
       [....]
   end;

initialization
   MySingleton := TSingleton.Create;

finalization
    MySingleton := nil;

end.




More information about the fpc-pascal mailing list