Discussion:
Typed constants question
(too old to reply)
J. Gareth Moreton
2018-07-28 13:12:28 UTC
Permalink
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
Marco van de Voort
2018-07-28 15:01:05 UTC
Permalink
Post by J. Gareth Moreton
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?
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin
J. Gareth Moreton
2018-07-28 15:30:48 UTC
Permalink
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
Post by J. Gareth Moreton
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?
Loading...