[fpc-devel] if-then-else expression
Sven Barth
pascaldragon at googlemail.com
Thu Aug 28 13:15:02 CEST 2025
Hairy Pixels via fpc-devel <fpc-devel at lists.freepascal.org> schrieb am Do.,
28. Aug. 2025, 09:55:
> On Aug 26, 2025 at 3:49:43 AM, Nikolay Nikolov via fpc-devel <
> fpc-devel at lists.freepascal.org> wrote:
>
>> But they're not 100% compatible, are they? IfThen evaluates both the true
>> and the false parameter, while the 'if' statement doesn't. For example:
>>
>> s := IfThen(b <> 0, a div b, 999)
>>
>> Will raise a division by zero exception, if b is 0, while
>>
>> s := if b <> 0 then a div b else 999
>>
>> won't.
>>
>> Nikolay
>>
>
> I think if the call was inlined it wouldn’t but otherwise it would. FPC
> can’t reliably inline either so you wouldn’t want to risk that.
>
That's wrong. Inlining does *not* change side effects (and if it does that
is considered a bug), so parameters are evaluated before the inlined
function's code is executed.
If that’s a concern then IfThen could be an intrinsic. It’s basically just
> a macro to a if statement with an assignment. Extremely simple thing to
> implement.
>
There had already been an IfThen-intrinisic almost ten years ago, which was
then rejected and reverted cause due to its different behavior to a
ordinary function would lead to more confusion.
The if-the-expression is just as easy to implement and is now required for
Delphi compatibility anyway, so there will not be any discussion about this
anymore.
Regards,
Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20250828/7b69615b/attachment.htm>
More information about the fpc-devel
mailing list