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
On 01.02.2016 21:14, Sven Barth wrote:

> Hello together!
>
> Time for the next flame. I've attached a patch which implements an "if
> Condition then ThenExpr else ElseExpr"-expression for those that want to
> play around with it. It follows the same principles as the
> yet-to-be-renamed IfThen() (namely that the type is determined by the
> ThenExpr) as it's just a different syntax with the same (copy & pasted)
> code behind it...
>
> Note: this patch *might* also work with 3.0.0 or even 2.6.4...
And I just noticed that I sent this to fpc-devel instead of
fpc-pascal... *sigh*

So here it is again for fpc-pascal as well.

Regards,
Sven


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

inline-if-expr.patch (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: if-then-else expression

Maciej Izak
2016-02-01 21:16 GMT+01:00 Sven Barth <[hidden email]>:
On 01.02.2016 21:14, Sven Barth wrote:
> Time for the next flame.

Flame? :O It was constructive discussion :)

For me

x := if true then 0 else 1

is ideal and compatible with Oxygene. +1 IMO no need to any other combination.

Maybe is time to start developing {$MODE OXYGENE} ?

--
Best regards,
Maciej Izak

_______________________________________________
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

silvioprog
On Mon, Feb 1, 2016 at 5:35 PM, Maciej Izak <[hidden email]> wrote:
2016-02-01 21:16 GMT+01:00 Sven Barth <[hidden email]>:
On 01.02.2016 21:14, Sven Barth wrote:
> Time for the next flame.

Flame? :O It was constructive discussion :)

+1. It was constructive discussion.

For me

x := if true then 0 else 1

For me, no problem extending ObjFPC to make a new abbreviated Pascal language, however, will be IfThen() function kept? (or something like this)
 
is ideal and compatible with Oxygene. +1 IMO no need to any other combination.

Maybe is time to start developing {$MODE OXYGENE} ?

--
Silvio Clécio

_______________________________________________
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

Michael Schnell
In reply to this post by Maciej Izak
On 02/01/2016 09:35 PM, Maciej Izak wrote:
>
>
> x := if true then 0 else 1

Here a keyword and a statement just optionally returns a value, Same is
ignored in all legacy code but can be use it you want to and do know
what you do.

Additionally "0" now is a statement, (optionally) extending what is
considered a statement.



This is a rather drastic change in the basic definition of the language,
but might be doable. I's completely optional, and I don't see how it
might break anything.

But of course the paradigm would ask for being extended to many or all
other keywords.

An obvious example is "case".

-Michael
_______________________________________________
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

Sven Barth-2
On 02.02.2016 10:25, Michael Schnell wrote:

> On 02/01/2016 09:35 PM, Maciej Izak wrote:
>>
>>
>> x := if true then 0 else 1
>
> Here a keyword and a statement just optionally returns a value, Same is
> ignored in all legacy code but can be use it you want to and do know
> what you do.
>
> Additionally "0" now is a statement, (optionally) extending what is
> considered a statement.

Why would "0" now be a statement? As long as the "if" is the expression
as well then the definition would be "if Condition then ThenExpr else
ElseExpr", not "if Condition then ThenStmt else ElseStmt". Otherwise
things like procedures ore assignments would be allowed as well which is
not the case.

>
> This is a rather drastic change in the basic definition of the language,
> but might be doable. I's completely optional, and I don't see how it
> might break anything.

Of course it's doable. See the patch I attached at the initial mail of
this thread.

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

John Lee
So (Sven) to clarify, assume that your original ifthen [patch is now in latest svn compiler although more work will needed to do x:=if a then b else c;? j 

On 2 February 2016 at 18:33, Sven Barth <[hidden email]> wrote:
On 02.02.2016 10:25, Michael Schnell wrote:
> On 02/01/2016 09:35 PM, Maciej Izak wrote:
>>
>>
>> x := if true then 0 else 1
>
> Here a keyword and a statement just optionally returns a value, Same is
> ignored in all legacy code but can be use it you want to and do know
> what you do.
>
> Additionally "0" now is a statement, (optionally) extending what is
> considered a statement.

Why would "0" now be a statement? As long as the "if" is the expression
as well then the definition would be "if Condition then ThenExpr else
ElseExpr", not "if Condition then ThenStmt else ElseStmt". Otherwise
things like procedures ore assignments would be allowed as well which is
not the case.

>
> This is a rather drastic change in the basic definition of the language,
> but might be doable. I's completely optional, and I don't see how it
> might break anything.

Of course it's doable. See the patch I attached at the initial mail of
this thread.

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


_______________________________________________
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

Sven Barth-2
On 02.02.2016 20:22, John Lee wrote:
> So (Sven) to clarify, assume that your original ifthen [patch is now in
> latest svn compiler although more work will needed to do x:=if a then b
> else c;? j

The IfThen() is in trunk though it will at least be renamed.
The if-then-else expression would in theory be good to go as well. Just
look at the first message of this thread for the patch to play around
with it.

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

geneb
On Tue, 2 Feb 2016, Sven Barth wrote:

> On 02.02.2016 20:22, John Lee wrote:
>> So (Sven) to clarify, assume that your original ifthen [patch is now in
>> latest svn compiler although more work will needed to do x:=if a then b
>> else c;? j
>
> The IfThen() is in trunk though it will at least be renamed.
> The if-then-else expression would in theory be good to go as well. Just
> look at the first message of this thread for the patch to play around
> with it.
>
I probably missed it, but what problem is "IfThen()" actually solving?

g.

--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
http://www.diy-cockpits.org/coll - Go Collimated or Go Home.
Some people collect things for a hobby.  Geeks collect hobbies.

ScarletDME - The red hot Data Management Environment
A Multi-Value database for the masses, not the classes.
http://scarlet.deltasoft.com - Get it _today_!
_______________________________________________
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

John Lee
In reply to this post by Sven Barth-2
Based on Florian's recent emails the if then else is ok, so guess we should be close to getting it all running. Great, thanks for your efforts. j  

On 2 February 2016 at 20:55, Sven Barth <[hidden email]> wrote:
On 02.02.2016 20:22, John Lee wrote:
> So (Sven) to clarify, assume that your original ifthen [patch is now in
> latest svn compiler although more work will needed to do x:=if a then b
> else c;? j

The IfThen() is in trunk though it will at least be renamed.
The if-then-else expression would in theory be good to go as well. Just
look at the first message of this thread for the patch to play around
with it.

Regards,
Sven

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


_______________________________________________
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
In reply to this post by geneb
On Tue, Feb 2, 2016 at 4:07 PM, geneb <[hidden email]> wrote:

I probably missed it, but what problem is "IfThen()" actually solving?

It's a sugar syntax, for

if cond then
  x:= a
else
  x:=b;

Pro #1: shortness of script
Pro #2: easier porting of C-style code (with ternary ? operator. Pascal doesn't have an equivalent https://en.wikipedia.org/wiki/%3F:#Programming_languages_without_the_conditional_operator )

thanks,
Dmitry



_______________________________________________
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

geneb
On Tue, 2 Feb 2016, Dmitry Boyarintsev wrote:

> On Tue, Feb 2, 2016 at 4:07 PM, geneb <[hidden email]> wrote:
>
> I probably missed it, but what problem is "IfThen()" actually solving?
>>
>> It's a sugar syntax, for
>
> if cond then
>  x:= a
> else
>  x:=b;
>
> Just follow this thread:
> http://lists.freepascal.org/pipermail/fpc-pascal/2016-January/046375.html
>
> Pro #1: shortness of script
> Pro #2: easier porting of C-style code (with ternary ? operator. Pascal

So it's not solving a /problem/ it's lessening an inconvienence(sp!).

I would've gone with IIf(). :)

g.

--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
http://www.diy-cockpits.org/coll - Go Collimated or Go Home.
Some people collect things for a hobby.  Geeks collect hobbies.

ScarletDME - The red hot Data Management Environment
A Multi-Value database for the masses, not the classes.
http://scarlet.deltasoft.com - Get it _today_!
_______________________________________________
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 Tue, Feb 2, 2016 at 4:40 PM, geneb <[hidden email]> wrote:
So it's not solving a /problem/ it's lessening an inconvienence(sp!).

I would've gone with IIf(). :)

Just keep in mind that the thread (about preferences) is closed.
Everyone are welcomed to create poll on the forum. Discussion also goes on at fpc-other list.

thanks,
Dmitry

_______________________________________________
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

geneb
On Tue, 2 Feb 2016, Dmitry Boyarintsev wrote:

> On Tue, Feb 2, 2016 at 4:40 PM, geneb <[hidden email]> wrote:
>
>> So it's not solving a /problem/ it's lessening an inconvienence(sp!).
>>
>> I would've gone with IIf(). :)
>>
>
> Just keep in mind that the thread (about preferences) is closed.
You can't "close" a discussion thread on an email list. :)

> Everyone are welcomed to create poll on the forum. Discussion also goes on
> at fpc-other list.

I don't have a dog in the fight, I was just making an observation.

g.

--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
http://www.diy-cockpits.org/coll - Go Collimated or Go Home.
Some people collect things for a hobby.  Geeks collect hobbies.

ScarletDME - The red hot Data Management Environment
A Multi-Value database for the masses, not the classes.
http://scarlet.deltasoft.com - Get it _today_!
_______________________________________________
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

Sven Barth-2

Am 02.02.2016 22:51 schrieb "geneb" <[hidden email]>:
>
> On Tue, 2 Feb 2016, Dmitry Boyarintsev wrote:
>
>> On Tue, Feb 2, 2016 at 4:40 PM, geneb <[hidden email]> wrote:
>>
>>> So it's not solving a /problem/ it's lessening an inconvienence(sp!).
>>>
>>> I would've gone with IIf(). :)
>>>
>>
>> Just keep in mind that the thread (about preferences) is closed.
>
> You can't "close" a discussion thread on an email list. :)

Yes, you can, because mails towards the list can be set to be moderated based on different criteria and I bet that the thread ID is one of them. Jonas (as list admin) probably did just that.

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

geneb
On Tue, 2 Feb 2016, Sven Barth wrote:

> Yes, you can, because mails towards the list can be set to be moderated
> based on different criteria and I bet that the thread ID is one of them.
> Jonas (as list admin) probably did just that.
>
Mailman (at least version 2.1.20, which is what I run) doesn't work like
that.

g.

--
Proud owner of F-15C 80-0007
http://www.f15sim.com - The only one of its kind.
http://www.diy-cockpits.org/coll - Go Collimated or Go Home.
Some people collect things for a hobby.  Geeks collect hobbies.

ScarletDME - The red hot Data Management Environment
A Multi-Value database for the masses, not the classes.
http://scarlet.deltasoft.com - Get it _today_!
_______________________________________________
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

etrusco
In reply to this post by Sven Barth-2


On Feb 2, 2016 7:41 PM, "geneb" <[hidden email]> wrote:
>
> (...)
>
> So it's not solving a /problem/ it's lessening an inconvienence(sp!).
>
> I would've gone with IIf(). :)
>
>
> g.
>

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);
2) or add inconsistency by using function syntax but different behavior.

Best regards,
Flávio


_______________________________________________
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

silvioprog
etrusco wrote
On Feb 2, 2016 7:41 PM, "geneb" <[hidden email]> wrote:
[...]
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:
A
10
----
B
A
10
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 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

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

silvioprog
On Wed, Feb 3, 2016 at 8:11 AM, Serguei TARASSOV <[hidden email]> wrote:
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

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

--
Silvio Clécio

_______________________________________________
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
In reply to this post by Serguei TARASSOV
On Wed, Feb 3, 2016 at 6:11 AM, Serguei TARASSOV <[hidden email]> wrote:
Holy sh*t, ça continue ! :)

thanks,
Dmitry

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