Re: [fpc-devel] Warning: Constructor should be public
2009/4/6 Flávio Etrusco : > Actually, your constructor has the same signature as 'TObject.Create', > so it should show a 'lower visibility' warning in Delphi, doesn't it? Correct, Delphi should also give this warning. > But, indeed, FPC shows the warning (should be public) for all > constructors, even if you're not hiding from the parent class, and I > always found this a (minor) annoyance. See my previous reply. Lowering visibility in descendant classes are breaking a contract (class interface) that was previously declared. Regards, - Graeme - ___ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Warning: Constructor should be public
On Mon, Apr 6, 2009 at 10:37 PM, Léo Willian Kölln wrote: > No. > See my singleton example: How did I guess it was going to be about implementing a Singleton. This issue has been raised a thousand times in FPC and Delphi circles. :) > > TLoggerGeral = class Which means you are descending from TObject. > private > {$IFDEF WINDOWS} >class var FInstance : TLoggerGeral; > {$ELSE} >{$STATIC on} >FInstance : TLoggerGeral; static; > {$ENDIF} > >constructor Create; Which means you are trying to lower the constructor TObject.Create from public to private. > > A normal singleton and it complain! I have a much easier "lazy man's" singleton for you... It's not 100%, but then, I have *never* seen a singleton implementation in Delphi or FPC that is 100%. Simply access the singleton via the gMySingleton method. eg: gMySingleton.DoSomethingCool; interface TMySingleton = class(TObject) ... end; function gMySingleton: TMySingleton implementation var uMySingleton: TMySingleton; function gMySingleton: TMySingleton; begin if not Assigned(uMySingleton) then uMySingleton := TMySingleton.Create; result := uMySingleton; end; [...snip the rest of TMySingleton implementation...] initialization uMySingleton := nil; finalization uMySingleton.Free; end. The reason you can't lower the visibility of the constructor or any method for that matter... A class interface (implementation section) , even for TObject as in your example, is a contract of how you can use that class and what is available to you. By lowering the visibility of a method or constructor in a descendant class, you are breaking the contract previously defined. This is a BIG no-no, hence the reason it is not allowed. Regards, - Graeme - ___ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Warning: Constructor should be public
Actually, your constructor has the same signature as 'TObject.Create', so it should show a 'lower visibility' warning in Delphi, doesn't it? But, indeed, FPC shows the warning (should be public) for all constructors, even if you're not hiding from the parent class, and I always found this a (minor) annoyance. But the fact that the semantic of constructors in Delphi/ObjectPascal have the same visibility rules of "common methods" (i.e. they force the visibility of the constructor from the parent class) is just sad. "Even" Java constructors' syntax is better ;-) Not to mention that you can call constructors on instance "pointer" (ok, I know this "hack is useful/used in streaming/persistence code), even uninitialized ones... -Flávio On Mon, Apr 6, 2009 at 5:37 PM, Léo Willian Kölln wrote: > No. > See my singleton example: > > TLoggerGeral = class > private > {$IFDEF WINDOWS} > class var FInstance : TLoggerGeral; > {$ELSE} > {$STATIC on} > FInstance : TLoggerGeral; static; > {$ENDIF} > > constructor Create; > > protected > pCritSection : TCriticalSection; > pLogarDatas : boolean; > pLogarTipos : boolean; > pArquivoDestino : TextFile; > saidaConsole : boolean; > > class function getData: String; > class function tipoText(tipo: ETipoMensagem): String; > class procedure executaOperacaoPrioridade(tipo: ETipoMensagem); > > public > destructor Destroy; override; > class function GetInstance: TLoggerGeral; > class procedure recebeMensagem(const mensagem: String; tipo: > ETipoMensagem = NORMAL); > end; > > A normal singleton and it complain! > > Léo Willian Kölln > > > On Mon, Apr 6, 2009 at 4:41 PM, Graeme Geldenhuys > wrote: >>> Simply question. Why? >> >> >> What did you do, try and lower the visibility of the constructor? If >> so, that is also not allowed in Delphi as far as I know. >> >> Regards, >> - Graeme - >> >> >> ___ >> fpGUI - a cross-platform Free Pascal GUI toolkit >> http://opensoft.homeip.net/fpgui/ >> ___ >> fpc-devel maillist - fpc-de...@lists.freepascal.org >> http://lists.freepascal.org/mailman/listinfo/fpc-devel >> > ___ > fpc-devel maillist - fpc-de...@lists.freepascal.org > http://lists.freepascal.org/mailman/listinfo/fpc-devel > ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Warning: Constructor should be public
No. See my singleton example: TLoggerGeral = class private {$IFDEF WINDOWS} class var FInstance : TLoggerGeral; {$ELSE} {$STATIC on} FInstance : TLoggerGeral; static; {$ENDIF} constructor Create; protected pCritSection: TCriticalSection; pLogarDatas : boolean; pLogarTipos : boolean; pArquivoDestino : TextFile; saidaConsole: boolean; class function getData: String; class function tipoText(tipo: ETipoMensagem): String; class procedure executaOperacaoPrioridade(tipo: ETipoMensagem); public destructor Destroy; override; class function GetInstance: TLoggerGeral; class procedure recebeMensagem(const mensagem: String; tipo: ETipoMensagem = NORMAL); end; A normal singleton and it complain! Léo Willian Kölln On Mon, Apr 6, 2009 at 4:41 PM, Graeme Geldenhuys wrote: >> Simply question. Why? > > > What did you do, try and lower the visibility of the constructor? If > so, that is also not allowed in Delphi as far as I know. > > Regards, > - Graeme - > > > ___ > fpGUI - a cross-platform Free Pascal GUI toolkit > http://opensoft.homeip.net/fpgui/ > ___ > fpc-devel maillist - fpc-de...@lists.freepascal.org > http://lists.freepascal.org/mailman/listinfo/fpc-devel > ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
Re: [fpc-devel] Warning: Constructor should be public
> Simply question. Why? What did you do, try and lower the visibility of the constructor? If so, that is also not allowed in Delphi as far as I know. Regards, - Graeme - ___ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel
[fpc-devel] Warning: Constructor should be public
Simply question. Why? In Delphi i don´t need this, and on ObjPascal i don´t see why this is necessary... This warning happens even when i use the Delphi compatibility mode! Léo Willian Kölln ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-devel