[fpc-devel] Pure function Wiki page

Thorsten Engler thorsten.engler at gmx.net
Mon Jul 9 00:47:49 CEST 2018


You are thinking about something like:

 

const

  x = FunctionCall(42);

 

In which case, yes, the compiler could possibly see that as in implicit “check if that function is pure”.

 

But “constant expressions” can also be something like:

 

If FunctionCall(42) > 0 then 

 

In which case you don’t want the compiler to have to test every single expression in your program to see if it is composed (all the way down) of pure functions.

 

For that, the pure; directive is essential, telling the compiler that it should try to evaluate that function at compile time (if called with only constant parameters).

 

And keeping this in mind, even for the first case (in an explicit const declaration) the compiler would only be marking the directly referenced function as “possibly pure”, but if that function internally calls other functions (which aren’t explicitly marked as pure) it would fail right away.  

 

From: fpc-devel <fpc-devel-bounces at lists.freepascal.org> On Behalf Of Dmitry Boyarintsev
Sent: Monday, 9 July 2018 08:39
To: FPC developers' list <fpc-devel at lists.freepascal.org>
Subject: Re: [fpc-devel] Pure function Wiki page

 

On Sun, Jul 8, 2018 at 5:43 PM, Thorsten Engler <thorsten.engler at gmx.net <mailto:thorsten.engler at gmx.net> > wrote:

People keep talking about keywords. As shown in the examples, pure is not a keyword. It's a context-sensitive directive. This is already wrongly stated in the proposal itself (so people can be excused for picking up on the use of the term "keyword" in the proposal) and it should be fixed (in the proposal).

And it's not redundant. You are telling the compiler: I want this function to be pure. Please tell me if I made a mistake.

 

If I put a function call into a constant expression, doesn't it already tell the compiler "I want this function to be pure. Please tell me if I made a mistake"?

 

thanks,

Dmitry

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180709/dc99b0d9/attachment.html>


More information about the fpc-devel mailing list