[fpc-pascal] New feature: IfThen() intrinsic
Jonas Maebe
jonas.maebe at elis.ugent.be
Tue Feb 2 12:27:42 CET 2016
Michael Van Canneyt wrote on Tue, 02 Feb 2016:
> On Tue, 2 Feb 2016, Henry Vermaak wrote:
>
>> On Tue, Feb 02, 2016 at 11:24:57AM +0100, Michael Van Canneyt wrote:
>>> On Tue, 2 Feb 2016, Henry Vermaak wrote:
>>>
>> Pascal is not a language full of programmer traps, let's try not to add
>> any!
>
> Well, see my reply about 'robust' code for an observation about
> these details.
You only talked about the order of evaluation in that mail, while
Henry is talking about whether the arguments are evaluated at all.
The parameter evaluation order issue is irrelevant in this context:
Delphi guarantees left-to-right (or used to anyway, this may have
changed), FPC doesn't guarantee anything.
There is, however, nothing fragile about writing code that assumes
that all arguments to a function/intrinsic are evaluated exactly once
(not more, not less), before the function is called, because they may
have side-effects. Our optimisation passes in the compiler also go to
great trouble to keep this property. You may not care about whether or
not all of the arguments to a function are evaluated or not, but it's
pretty fundamental.
As was mentioned on the core list, there are currently two exceptions
to this rule:
a) assert, because of its C-macro origin (it only evaluates the second
argument if the first evaluates to true)
b) read(ln)/write(ln), because e.g. writeln(x,y,z) is specified in the
original Pascal standard as being equivalent to
write(x);
write(y);
write(z);
writeln;
(which means that if e.g. y is a function call and you also write
something inside that function, it will appear after x has been
written -- while it would appear before if writeln observed the normal
function call semantics)
As Henry noted, depending on whether you will be using the built-in
iif/ifthen intrinsic or an iif/ifthen defined in another unit, whether
or not all arguments will be evaluated changes (this also holds for
assert, FWIW). That is not good, especially since in pretty much all
other languages that have an ifthen/iif intrinsic, all arguments /are/
always evaluated (unlike with assert in other languages).
Jonas
More information about the fpc-pascal
mailing list