[fpc-pascal] New feature: IfThen() intrinsic
Michael Van Canneyt
michael at freepascal.org
Tue Feb 2 12:36:04 CET 2016
On Tue, 2 Feb 2016, Henry Vermaak wrote:
> On Tue, Feb 02, 2016 at 12:03:14PM +0100, Michael Van Canneyt wrote:
>> On Tue, 2 Feb 2016, Henry Vermaak wrote:
>>> When you call a function all the arguments get evaluated before the
>>> function gets called. iif will look exactly like a function call,
>>> but all the arguments may not be evaluated (that's the point, right).
>>> This is inconsistent and ambiguous, two things that pascal tries to
>>> eschew.
>>>
>>> Even worse is that it's not a reserved symbol, so it can still be
>>> overridden. A call to iif in one unit may evaluate all its
>>> arguments, while it may not in another unit.
>>>
>>> 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.
>
> There you talk about order of evaluation, which is irrelevant to what
> I'm trying to say. Maybe I'll try an example to explain what I'm on
> about.
>
> Say you have code like this:
>
> IfThen(IsSomething, MungeA(A), MungeB(B));
>
> Currently MungeA(A) and MungeB(B) are called, irrespective of the value
> of IsSomething. The order is irrelevant (I always assumes that it was
> undefined, like in C) and I agree that it's wrong to rely on.
>
> If you replace it with the new IfThen (or iif), only MungeA(A) _or_
> MungeB(B) will be called. This is exactly what you want from a
> conditional operator.
>
> The conditional operator shouldn't look like a function call, because it
> doesn't act like a function call: it will not evaluate all its
> arguments. Again: nothing to do with order, it's whether they actually
> get evaluated in the first place.
Yes.
As I wrote to Jonas, it doesn't change one iota about my observation.
It only reminds me that I need to be more exact in my formulation.
Michael.
More information about the fpc-pascal
mailing list