Sign in Join

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

Mailing lists / fpc-pascal / 2016-02

Re: New feature: IfThen() intrinsic

4 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 12 lines
On Tue, 2 Feb 2016, Michael Van Canneyt wrote > > On Tue, 2 Feb 2016, Serguei TARASSOV wrote: > No, the second is always better because safer. > > > >x := iif (Obj = nil, 0, Obj.Value); > >This will raise access violation as a normal function or you depend on > >compiler implementation for this special case. And you should remember an > >additional special case of function. > > > >x := iif Obj = nil then 0 else Obj.Value; > >Doesn't raise AV because it's a statement, not function. It is not a statement, it is an expression.

???
iif is a new statement "inline if".
It contains other statements "then" "else"
It returns a value like an assignment statement ":=" in Pascal or "return" in C-like languages.
It may be used without assignment:

{$IFDEF DEBUG}
x :=
{$ENDIF}
iif Obj = nil then 0 else Obj.DoSomethingAndReturnValue;
I prefer x := iif Obj = nil then 0 else Obj.Value; over x := if Obj = nil then 0 else Obj.Value;

It's exactly that I suggest.

Show quoted text 6 lines
Because it clearly differentiates between if (a statement) and iif (an expression) So the iif in either functional or expression form has my vote. Michael.

Regards,
Serguei

Michael Van Canneyt
to Serguei TARASSOV
Serguei TARASSOV wrote 26 lines
On 02/02/2016 10:54, wrote: > On Tue, 2 Feb 2016, Michael Van Canneyt wrote >> >> On Tue, 2 Feb 2016, Serguei TARASSOV wrote: > No, the second is always >> better because safer. >> > >> >x := iif (Obj = nil, 0, Obj.Value); >> >This will raise access violation as a normal function or you depend on >> >compiler implementation for this special case. And you should remember an >> >additional special case of function. >> > >> >x := iif Obj = nil then 0 else Obj.Value; >> >Doesn't raise AV because it's a statement, not function. > It is not a statement, it is an expression. ??? iif is a new statement "inline if". It contains other statements "then" "else" It returns a value like an assignment statement ":=" in Pascal or "return" in C-like languages. It may be used without assignment: {$IFDEF DEBUG} x := {$ENDIF} iif Obj = nil then 0 else Obj.DoSomethingAndReturnValue;

I doubt this is what Sven implemented/will implement. The assignment will always be needed.
But Sven will have to comment on that.

Show quoted text 6 lines
> I prefer > x := iif Obj = nil then 0 else Obj.Value; > over > x := if Obj = nil then 0 else Obj.Value; It's exactly that I suggest.

I know.

Michael.

Sven Barth
to Serguei TARASSOV: ??? iif is a new statement "inline if". It contains other statements "then" "else" It…
"Serguei TARASSOV" wrote 7 lines
On 02/02/2016 10:54, wrote: > On Tue, 2 Feb 2016, Michael Van Canneyt wrote >> >> >> On Tue, 2 Feb 2016, Serguei TARASSOV wrote: > No, the second is always

better because safer.

Show quoted text 5 lines
>> >> > >> >x := iif (Obj = nil, 0, Obj.Value); >> >This will raise access violation as a normal function or you depend on >> >compiler implementation for this special case. And you should remember

an

Show quoted text 9 lines
>> >additional special case of function. >> > >> >x := iif Obj = nil then 0 else Obj.Value; >> >Doesn't raise AV because it's a statement, not function. > > It is not a statement, it is an expression. ??? iif is a new statement "inline if".

No, it's an expression, not a statement.

It contains other statements "then" "else"

No, these are keywords.

It returns a value like an assignment statement ":=" in Pascal or

"return" in C-like languages.

That's why it is an expression. Statements don't return values.

Show quoted text 6 lines
It may be used without assignment: {$IFDEF DEBUG} x := {$ENDIF} iif Obj = nil then 0 else Obj.DoSomethingAndReturnValue;

No, because "0" is not a valid statement.

Regards,
Sven

Sven Barth
to Michael Van Canneyt: I doubt this is what Sven implemented/will implement. The assignment will always be…
"Michael Van Canneyt" wrote 11 lines
On Tue, 2 Feb 2016, Serguei TARASSOV wrote: > On 02/02/2016 10:54, wrote: > >> On Tue, 2 Feb 2016, Michael Van Canneyt wrote >>> >>> >>> On Tue, 2 Feb 2016, Serguei TARASSOV wrote: > No, the second is always

better because safer.

>>> > >>> >x := iif (Obj = nil, 0, Obj.Value); >>> >This will raise access violation as a normal function or you depend on >>> >compiler implementation for this special case. And you should

remember an

Show quoted text 11 lines
>>> >additional special case of function. >>> > >>> >x := iif Obj = nil then 0 else Obj.Value; >>> >Doesn't raise AV because it's a statement, not function. >> >> It is not a statement, it is an expression. > > ??? > iif is a new statement "inline if". > It contains other statements "then" "else" > It returns a value like an assignment statement ":=" in Pascal or

"return" in C-like languages.

Show quoted text 9 lines
> It may be used without assignment: > > {$IFDEF DEBUG} > x := > {$ENDIF} > iif Obj = nil then 0 else Obj.DoSomethingAndReturnValue; I doubt this is what Sven implemented/will implement. The assignment will

always be needed.

But Sven will have to comment on that.

Indeed.

Regards,
Sven