[fpc-pascal] Initialization of constant record member of pointer type

Michael Van Canneyt michael at freepascal.org
Tue Dec 1 12:13:33 CET 2020



On Tue, 1 Dec 2020, Ladislav Karrach via fpc-pascal wrote:

>
>>
>> Because MyConst1 is not an *untyped* constant. Only untyped constants 
>> can be used in constant expressions (a pointer to something can be 
>> considered an untyped constant).
>>
>> The following might work though I did not test it:
>>
>> === code begin ===
>>
>> const
>>    MyStr = 'abc'
>>    MyConst1: AnsiString = MyStr;
>>    MyConst2: TMyRec = (l: Length(MyStr); a: @MyConst1[1]);
>>
>>
>> === code end ===
>>
> Yes it works, but I must define 2 constants (MyStr and MyConst1), which 
> is not so nice ;-)
>
> It would be nice to have support for true constants:
>
> const
>    MyConst1 = 'abc' ;
>    MyConst2: TMyRec = (l: Length(MyConst1); a: @MyConst1[1]);
>
> But may be that there are technical reasons why it is problematic (may 
> be that true constants are stored in another memory locations, which can 
> not be easy addressed)

>From a pure language point of view, true constants do not "exist". 
They are substituted wherever the compiler finds a reference. 
So there is no address in memory.

A typed constant is like a variable (it is the precursor of the initialized
variable), it has an address in memory.

Michael.


More information about the fpc-pascal mailing list