[fpc-pascal] Writeable typed constants - what's the point?

Graeme Geldenhuys graemeg at opensoft.homeip.net
Thu Jun 18 08:44:22 CEST 2009


Hi,

Recently I learned that you can have writeable typed constants. That
sounds rather like an oxymoron to my. Writeable constants make no sense,
is that then simply a variable? What is the use of a writeable typed
constant?

Below is two examples of how they can be used, and support for them are
toggled with the compiler directive $J.

In both these cases I would think a variable would make a lot more
sense. For the Singleton implementation, you simply need to specify a
unit wide AppSingleton variable in the implementation section (so that
it's not completely global or visible to other units.

Can anybody explain the point of writeable typed constants?

const
   foo: Integer = 12;
begin
   foo := 14;
end.

===============================

// Singleton implementation using typed constants
function gAppManager: TApplicationManager;
const
  {$IFDEF DELPHI}{$J+}{$ENDIF}
  AppSingleton: TApplicationManager = nil;
  {$IFDEF DELPHI}{$J-}{$ENDIF}
begin
  if not Assigned(AppSingleton) then
    AppSingleton := TApplicationManager.Create;
  Result := AppSingleton;
end;


Regards,
  - Graeme -

_______________________________________________________
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/




More information about the fpc-pascal mailing list