[fpc-devel] class constants

Peter Popov ppopov99 at gmail.com
Mon Oct 19 16:18:15 CEST 2009


>
> An example would be
> TItemCache = class
> private
>    const DEFAULT_CACHE_SIZE = 1024;
>   ...
> end;
>
> sure a global value would do, but there is no need for the value to be  
> global.
>
> Martin

This is a good example. However, the same paradigm can be achived by a  
function:

function TItemCache.DEFAULT_CACHE_SIZE: Integer;
begin
   Result := 1024;
end;

If properly inlined, the compiled code is the same and semantically the  
two are the same.
Granted, defining it as constant saves the 4 lines needed to implement the  
function :-)

In the same example, a typed global constant gives it a different flavour,  
in the sense that the user of the class can modify the value at run-time,  
e.g. by reading an ini file/registry entries when the app starts.

Peter



More information about the fpc-devel mailing list