Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2018-07

Typed constants question

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

J. Gareth Moreton

So I have a question about constants.

While programming pure functions, I noticed that typed and untyped constants are treated differently.  Something like "const Pi = 3.14159", even if declared outside of the current function, is propagated directly in expressions - the node construction shows it loading the actual value.  However, when you have something like "Const Pi: Double = 3.14159", it instead accesses the symbol like a non-local variable. Normally I would treat accessing such a variable as making the function ineligible to be pure (as it's not self-contained), but is it safe to make an exceptional case for constants, or is there something I'm missing (e.g. writable constants).

For a more fundamental question... why are typed and untyped constants treated differently?

Gareth aka. Kit

Reply by email

Marco van de Voort
to J. Gareth Moreton

In our previous episode, J. Gareth Moreton said:

For a more fundamental question... why are typed and untyped constants treated differently?

Turbo Pascal allowed to write them, depending on a switch. $J. Delphi adds {$writeableconst on} iirc.

But even when this is off, you can't be sure it is on in a different module, so exported constants can never be folded in. (except of course as a global optimizations)

I guess for a local (implementation) constant this could be checked?

Reply by email

J. Gareth Moreton

Given I want to be conservative with pure functions so it's near-impossible to write something that breaks the compiler or cause unexpected behaviour in the compiled binary, I'll treat the accessing of such a constant as "impure" for reasons of safety.  My work-in-progress already detects accessing a non-local variable, and this also covers typed constants, and will remove the 'pure' flag and throw a compiler warning in this case.

Because I'm the developer of this feature, I can't guarantee that I won't overlook something when it comes to writing unit tests.  I'll probably be relying on you guys to push the compiler and the 'pure' feature to breaking point!

Gareth aka. Kit

On Sat 28/07/18 16:01 , (Marco van de Voort) sent: In our previous episode, J. Gareth Moreton said:

For a more fundamental question... why are typed and untyped constants treated differently?

Turbo Pascal allowed to write them, depending on a switch. $J. Delphi adds {$writeableconst on} iirc.

But even when this is off, you can't be sure it is on in a different module,
so exported constants can never be folded in. (except of course as a global
optimizations)

I guess for a local (implementation) constant this could be checked?

Reply by email