[fpc-devel] Delphi anonymous methods

Sven Barth pascaldragon at googlemail.com
Tue Mar 5 10:27:27 CET 2013


Am 05.03.2013 10:20, schrieb Graeme Geldenhuys:
> On 2013-03-04 20:33, Howard Page-Clark wrote:
>> You can simulate this in FPC as well as TP by using a local typed
>> constant. e.g.
>>
>> function GetValue: integer;
>> const value: integer = 0;
>> begin
>>     Inc(value);
>>     Result:= value;
>> end;
>
> I've seen this before, and always been baffled by this. How can you
> increment a "constant"? If you can, it is then a variable, no?
In Turbo Pascal these typed constants were used as the poor man's 
visibility managment. You basically had a global variable that only that 
function (and nested ones) can access.

In Delphi the $J switch was introduced which is disabled by default, 
which means that typed constants are not writeable. You can nevertheless 
switch it on.

Note: The important point here is that it is a typed constant. And there 
typed constants and variables are technically the same (both are located 
in the initalized read/write area of the executable file), while true 
constants are either inserted directly into the code (ordinals, floating 
point values) or are located in a read only area of the executable 
(strings) [if the format does not support a readonly area they are 
located in the read/write one as well though].

Regards,
Sven



More information about the fpc-devel mailing list