[fpc-devel] Standard generic classes
Sven Barth
pascaldragon at googlemail.com
Wed Jun 5 23:38:39 CEST 2019
J. Gareth Moreton <gareth at moreton-family.com> schrieb am Mi., 5. Juni 2019,
18:06:
>
> *for *X := 0 *to *Min(BUFFER_SIZE, DataLeft) *- 1 do*
> *begin*
> { Do some work that doesn't modify DataLeft }
> *end*;
>
> Say that Min is given the *pure *directive, BUFFER_SIZE is a *const *and
> DataLeft is a property or some non-local variable. Since no volatile
> intrinsic is used to safeguard multi-threading issues, and DataLeft isn't
> modified by the for-loop, DataLeft can be promoted to the stack or a
> register. Even though its exact value is not known, DataLeft is determined
> to be constant within the confines of the for-loop, hence since both
> arguments in "Min(BUFFER_SIZE, DataLeft)" are constants, the result must
> also be constant, since it's a pure function, therefore the function only
> has to be called once and its result be stored on the stack, recalled
> during each iteration of the for-loop. At least that's the theory. Of
> course, you can just store the result yourself in a local variable and use
> that as part of the for-loop, but it's just a thought where pure functions
> could be used that doesn't involve computing their result at compile-time.
>
With properties or global variables you can't guarantee that DataLeft is
constant as the for loop might call some routine that more or less
indirectly might change what DataLeft returns.
That said: the end-expression of a for-loop is specified to be evaluated
only once anyway (in contrast to C-like languages).
Regards,
Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190605/c2f5f703/attachment.html>
More information about the fpc-devel
mailing list