[fpc-devel] Pure function development discussion
J. Gareth Moreton
gareth at moreton-family.com
Sun Aug 12 01:48:46 CEST 2018
Hi everyone. The development of pure function support is going well,
along with catching error cases. Thanks to some discussion from Sven, the
compiler will now detect if you reference a function before defining it as
"pure", so cases where it's defined as "forward" are trapped as errors so
as to not cause problematic behaviour. I'm still a little unsure of it in
places, but so far, I have been running the attached file through the
compiler.
PureTestUnit.pas(5,10) Hint: Function "PureMin" is eligible for the "pure"
directive
PureTestUnit.pas(33,15) Warning: Procedure is not eligible to be pure
(impure function call)
PureTestUnit.pas(37,1) Error: Function declared pure after it has already
been referenced
PureTestUnit.pas(44,4) Fatal: There were 1 errors compiling module,
stopping
In this situation, the parsing of the "Binomial" function, which has been
declared to be pure, raises a warning because it calls the Factorial
function, which hasn't been declared as such in the interface, but later on
in the implementation, and then the error is because the implementation of
Factorial appears after it's already been used. If you rearrange the code
so Factorial appears before Binomial, the error and warning vanish.
Currently that feels a bit clumsy to me.
One question I've yet to find an answer for is when you have something
like the following:
interface
function ln(x: Double): Double; pure;
const
LN2 = ln(2);
implementation
function HalfLife(decay: Double): Double;
begin
Result := LN2 / decay;
end;
function ln(x: Double): Double;
begin { Code to calculate natural lograithm }
end;
How and when is the constant defined? Should such constructs be disallowed
and constants only allowed to be declared in other units that use the unit
that contains the pure functions in its interface section? What would be
the ideal and cleanest behaviour?
Gareth aka. Kit
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180812/2ac6e5e9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PureTestUnit.pas
Type: application/octet-stream
Size: 964 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180812/2ac6e5e9/attachment.obj>
More information about the fpc-devel
mailing list