[fpc-pascal] Const attributes

Sven Barth pascaldragon at googlemail.com
Sun Jul 30 22:47:11 CEST 2017


On 30.07.2017 20:55, Marcos Douglas B. Santos wrote:
> On Sun, Jul 30, 2017 at 1:46 PM, Maciej Izak <hnb.code at gmail.com> wrote:
>> 2017-07-30 18:37 GMT+02:00 Marcos Douglas B. Santos <md at delfire.net>:
>>>
>>> 1. Is this by design?
>>
>>
>> Yes. To disallow this you need to use {$J-} directive.
> 
> I know that we can use this "hack" in local functions/methods, but in
> an attribute... What is the goal to use it?

$J affects all kinds of constants no matter whether they are global,
inside a routine or inside a type. Though it seems that this is broken
as the compiler allows an assignment nevertheless... *sigh*

>>>
>>> 2. Is there a way to declare attributes "final" like Java does?
>>
>>
>> No for constants.
> 
> I would like to instantiate my attribute only once inside constructor
> and then it will be "const" or "final", I mean, immutable.
> Today it is not possible, right? Any thoughts to the future?

With the JVM platform support for final fields was added (modeswitch
finalfields), but that is currently only available for external classes
(which only exist for the JVM target) as the DFA can't handle that
correctly currently.
(though if one would restrict the assignments to the constructors itself
without any methods called by the constructor then we might be able to
implement that nevertheless, don't know what Java or other languages
with final fields allow here)

Regards,
Sven


More information about the fpc-pascal mailing list