[fpc-pascal] Implementing a true Singleton - Can we decrease the visibility of a method?
Jonas Maebe
jonas.maebe at elis.ugent.be
Fri Dec 8 14:20:25 CET 2006
On 8 dec 2006, at 14:03, Graeme Geldenhuys wrote:
> There is a lot of things in life we shouldn't do, but we do. :-)
> At least give us the choice, maybe via a compiler directive and keep
> the compiler warning in place. That way we can use it responsibly
> when required,
This is a generic argument which you can use in support of pretty
much any relaxing of semantic/syntax rules.
> as in the case of implementing a true singleton. I'm
> sure there are more examples. As it stands currently (my sample
> singleton code) the developer can still screw up by creating a new
> instance instead of going through the global function.
This particular issue is caused by the fact that in Delphi-style
Object Pascal classes automatically inherit from TObject, which has a
public constructor. And if you override the constructor with your
own, they no longer can do that either (although it's obviously a run
time and not compile time check).
One thing that could be changed is the compile so that you get a
compile-time error if you try
type
tc = class
constructor create; virtual; abstract;
end;
var
c: tc;
begin
c := tc.create
end.
(currently this compiles with only a warning about the fact that tc
has abstract methods)
It would however still only give a run time error if you'd instead do
type
tc = class
constructor create; virtual; abstract;
end;
ttc = class of tc;
var
cc: ttc;
c: tc;
begin
cc := tc;
c := cc.create;
end.
Jonas
More information about the fpc-pascal
mailing list