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

4 messages · 4 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 16 lines
Date: Tue, 2 Feb 2016 19:43:02 -0700 (MST) From: silvioprog > >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

Regards,
Serguei

Reply by email

silvioprog
to Serguei TARASSOV
Serguei TARASSOV wrote 23 lines
On 03/02/2016 12:00, wrote: > Date: Tue, 2 Feb 2016 19:43:02 -0700 (MST) > From: silvioprog > > >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 ! :)

:-)

Show quoted text 7 lines
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

Did you test it? Using the current System.IfThen() it doesn't raises AV for me.

Silvio Clécio

Reply by email

Sven Barth
to Serguei TARASSOV: Holy sh*t, ça continue ! :) Even if evaluation order will be assured and well…
"Serguei TARASSOV" wrote 9 lines
On 03/02/2016 12:00, wrote: > > Date: Tue, 2 Feb 2016 19:43:02 -0700 (MST) > From: silvioprog > > >> >The problem with Iff() is:1) it either retains normal function behavior >> >and thus has to evaluate both expressions (i.e. suboptimal performance

and

Show quoted text 15 lines
>> >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

Reply by email