[fpc-pascal] Read-only global references

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sat Mar 5 23:05:14 CET 2011


Marcos Douglas wrote:
> On Sat, Mar 5, 2011 at 8:24 AM, Mark Morgan Lloyd
> <markMLl.fpc-pascal at telemetry.co.uk> wrote:
>> Where a unit exports an instance of an object, what's best practice for
>> making this read-only? I'm referring to the object reference itself here,
>> not properties of the object.
>>
>> Where I've done this in the past I've used a function:
>>
>> interface
>>
>> function InitText: TFormattedString;
>>
>> implementation
>>
>> var     xInitText: TFormattedString= nil;
>>
>> function InitText: TFormattedString;
>>
>> begin
>>  result := xInitText
>> end { InitText } ;
>>
>> Is there a better way using e.g. global properties that doesn't necessitate
>> both a property and an explicit function?
>>
>> property InitText: TFormattedString read xInitText;
>>
>> The obvious problem here is that xInitText is a forward reference to an
>> unexported variable.
> 
> Make a function to return a global variable does not make it read only.
> If I use your function like this:
> o := InitText;
> o := nil;  //this is the same xInitText = nil

Disagree. You are changing the value of o, not of xInitText. xInitText 
is initialised once somewhere in the unit exporting it, subsequently its 
properties etc. can be used elsewhere e.g. InitText.AppendFormatted(...).

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list