Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-02

Re: if-then-else expression

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Serguei TARASSOV
Show quoted text 17 lines
Date: Wed, 3 Feb 2016 14:34:34 +0100 From: Sven Barth Am 03.02.2016 12:11 schrieb "Serguei TARASSOV": >Holy sh*t, ?a continue !:) >Even if evaluation order will be assured and well documented, it doesn't make sense! > >Example : > > > >x := iif(Obj = nil, 0, Obj.Value); // Seems OK when right-to-left and stop on 'true' evalation > >x := iif(Obj nil, Obj.Value, 0); // Raise access violation The current IfThen() intrinsic is not a function call. Internally it gets replaced by an if-node thus from the compiler's point of view it behaves like an if-statement that assigns the expressions in its branches to a temporary variable (yes that one is thrown away during code generation...), thus the term evaluation order in the sense of calling functions does not apply at all anyway.

I see, and it is what I said many times: you wrap the if-statement into pseudo-function.
This make no sense and will be source of programmer's errors. The simplest case is to add "uses Math" or "uses StrUtils" to hide intrinsic pseudo-function and crash the application.

iif bool-expression then expression-true else expression-false;
or
inline if bool-expression then expression-true else expression-false;

is only solution in my opinion.

Regards,
Serguei

Jonas Maebe
to Serguei TARASSOV
Serguei TARASSOV wrote
I see, and it is what I said many times

And since this is now becoming a thread in which mostly things from the previous one are repeated, please also move it to the fpc-other list.

Thanks,

Jonas
FPC mailing lists admin