[fpc-devel] if-then-else expression
Nikolay Nikolov
nickysn at gmail.com
Tue Aug 26 11:49:43 CEST 2025
On 8/25/25 5:27 PM, Hairy Pixels via fpc-devel wrote:
> On Aug 24, 2025 at 3:28:45 PM, Karoly Balogh via fpc-devel
> <fpc-devel at lists.freepascal.org> wrote:
>> The new syntax has strong Python vibes to me. This is where I saw/used
>> this the most. Which is not necessarily a bad thing. My code is usually
>> very oldschool and I tend to avoid most of the "modern" bells & whistles,
>> as I have a firm belief they hinder long-term maintainability, or they're
>> simply eye-sore (mainly that Delphi inline variable declaration comes to
>> mind) but this is one of the "newschool" syntax things which I'd gladly
>> use myself.
>
> I think they added a IfThen generic which can be implicitly
> specialized now so you can do things like:
>
> s := IfThen(p <> nil, 'valid', 'nil’);
> vs
>
> s := If p <> nil then 'valid' else 'nil';
>
> It’s almost easier to read. I’m not sure I’d like the have
> if-statements confused with expressions when scanning code either. At
> least the C version is more terse but that one can be hard to identify
> also. All in all I think IfThen is actually pretty good.
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20250826/b64c522a/attachment.htm>
More information about the fpc-devel
mailing list