Re: if-then-else expression

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: if-then-else expression

Sven Barth-2

Am 03.02.2016 12:11 schrieb "Serguei TARASSOV" <[hidden email]>:
>
> On 03/02/2016 12:00, [hidden email] wrote:
>>
>> Date: Tue, 2 Feb 2016 19:43:02 -0700 (MST)
>> From: silvioprog<[hidden email]>
>>
>>
>>> >The problem with Iff() is:1) it either retains normal function behavior
>>> >and thus has to evaluate both expressions (i.e. suboptimal performance and
>>> >allowing side effects);
>>
>> Well:
>> program Project1;  function test1: integer;  begin    WriteLn('A');
>> Result := 10;  end;  function test2: integer;  begin    WriteLn('B');
>> Result := 20;  end;  function CommonFunc(A: Boolean; B, C: integer):
>> integer;  begin    if A then      Result := B    else      Result := C;
>> end;var  X: LongInt;begin  X := IfThen(True, test1, test2);  WriteLn(X);
>> WriteLn('----');  X := CommonFunc(True, test1, test2);  WriteLn(X);
>> ReadLn;end.
>> Result:
>> A10----BA10
>
> 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.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: if-then-else expression

Serguei TARASSOV
In reply to this post by Sven Barth-2
On 04/02/2016 12:00, [hidden email] wrote:

> Date: Wed, 3 Feb 2016 14:34:34 +0100
> From: Sven Barth<[hidden email]>
>
> Am 03.02.2016 12:11 schrieb "Serguei TARASSOV"<[hidden email]>:
> >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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
--
Regards,
Serguei
Reply | Threaded
Open this post in threaded view
|

Re: if-then-else expression

Jonas Maebe-2
On 04/02/16 12:36, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: if-then-else expression

dmitry boyarintsev
On Thu, Feb 4, 2016 at 6:42 AM, Jonas Maebe <[hidden email]> wrote:
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.

Of the forum: http://forum.lazarus.freepascal.org/index.php/topic,31367.0.html


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12