[fpc-devel] Pure function Wiki page

J. Gareth Moreton gareth at moreton-family.com
Sun Jul 8 18:15:12 CEST 2018


 It also feels safer and more intuitive for the programmer.  If you're
writing code such as "const Partitions = log2(Length(StaticList));"
(assuming Length(StaticList) evaluates to a constant value), then defining
log2 as a pure function shows you know what you're doing, because otherwise
it might seem random and arbitrary in that you can assign some functions to
constants, but not others.  And if the pure function in question is
actually impure, then you get both a warning and an error... first the
warning that the function is impure, and then an error because you're
assigning a function result to a constant, and the two appearing
side-by-side will quickly inform the programmer what's going on.
 It's also the reason behind my choice of the pure keyword... ease-of-use
and intuitiveness.

 Gareth

 On Sun 08/07/18 18:01 , Florian Klämpfl florian at freepascal.org sent:
 Am 08.07.2018 um 18:53 schrieb Dmitry Boyarintsev: 
 > On Sun, Jul 8, 2018 at 11:20 AM, J. Gareth Moreton  wrote: 
 > 
 > As far as I know, keywords are often used (e.g. "constexpr" in C++). 
My reasons are explained in the Wiki topic, 
 > but it boils down to compiler performance. 
 > 
 > How about adding a new modeswitch instead PUREFUNCTIONS? 
 > The mode switch would treat any "function" as pure function (and should
check the code for "purity"). 
 > 
 > While at the same time would allow "procedures" to return values: 
 > procedure Random(l: LongInt):LongInt; 
 > 
 > However, not having an extra modeswitch or new keywords would be
beneficial: 
 > 
 > From the compiler performance perspective, the purity of a function
needs to be checked only when evaluating constant 
 > expressions (according to the wiki article) 
 > Thus the actual use of a function in a constant expression should act as
the proposed "pure" keyword. 
 > 

 No. Because pure is part of the function header and tells users "you can
use this function with constant arguments in 
 constant expressions and this won't change without notification". If the
compiler determines by itself if a function is 
 pure or not, it might even depend on the compiler version if a function is
detected as pure or not. 
 _______________________________________________ 
 fpc-devel maillist - fpc-devel at lists.freepascal.org [1] 
 http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[2]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel 

 

Links:
------
[1] mailto:fpc-devel at lists.freepascal.org
[2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180708/ad417d09/attachment.html>


More information about the fpc-devel mailing list