Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Vojtěch Čihák

Hi,
 
what is difference in produced assembler between ifthen(); and classic if - 
then - else?

Thanks, V.
__

Od: Sven Barth 
Komu: "FPC-Pascal users discussions" 
Datum: 31.01.2016 15:43
Předmět: [fpc-pascal] New feature: IfThen() intrinsic


Hello together!

I've finally come around to add a feature that many people have asked
for throughout the years: an inline if that works like the if-statement
in that it only evaluates that expression that is indeed returned.

After the discussion last year about the inline-if I've decided to add
it as an intrinsic function instead of an extension of the language.
Like all intrinsics it's part of the System unit and "declared" like this:

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Mark Morgan Lloyd

Sven Barth wrote:

Hello together!

I've finally come around to add a feature that many people have asked
for throughout the years: an inline if that works like the if-statement
in that it only evaluates that expression that is indeed returned.

After the discussion last year about the inline-if I've decided to add
it as an intrinsic function instead of an extension of the language.


Thanks for that, it will make maintaining some Pascal transcribed from 
Javascript much easier.


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Sven Barth
On 31.01.2016 16:14, Vojtěch Čihák wrote:
> what is difference in produced assembler between ifthen(); and classic
> if - then - else?

=== code begin ===

i := IfThen(x < 42, 32, 49);

=== code end ===

is equivalent to

=== code begin ===

if x < 42 then
  i := 32
else
  i := 49;

=== code end ===

That's in fact how the compiler handles this internally (with the added
benefit of nesting IfThen()s).

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Sven Barth
On 31.01.2016 16:09, silvioprog wrote:
> Wooow! I did a request about it many years ago, however it was rejected
> because the limitation in the generic feature at that time.

This implementation has nothing to do with generics. It's a compiler
magic function, just like Writeln() and thus it can bend the usual rules ;)

> So, I posted a bug related to it yesterday:
> 
> http://bugs.freepascal.org/view.php?id=29546

I've seen your bug this night and that was the cause for finally
implementing this.

> Can I use it after this new feature? (I'm upgrading my FPC sources now :-) )

I've not yet looked at what the problem regarding your code is. However
you can simply remove your TUtils.Iif() and use IfThen() instead.

> Buddy, thanks a lot for that feature, it is very useful! o/

That's what I have hoped for. :)

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Ralf Quint

On 1/31/2016 7:14 AM, Vojtěch Čihák wrote:


Hi,

what is difference in produced assembler between ifthen(); and classic 
if - then - else?




+1

I don't really see how this is different from properly writing  if ... 
then ... else... either... :-\


Ralf

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Marco van de Voort
In our previous episode, Sven Barth said:
> After the discussion last year about the inline-if I've decided to add
> it as an intrinsic function instead of an extension of the language.
> Like all intrinsics it's part of the System unit and "declared" like this:
> 
> === code begin ===
> 
> function IfThen(Condition: Boolean; ThenExpr, ElseExpr: type): type;
> 
> === code end ===
> 
> Since it's declared in the System unit it won't interfere with the
> IfThen()s that are declared in the Math unit or other units, so they'll
> continue to work as before to avoid any surprises.
> 
> An important point to note is that in general the result type is
> determined by the ThenExpr (there are a few usability exceptions
> regarding strings and chars).

I'm not really fond of the functionality, and I would not write new code
with it, but it is very handy when converting C code.
 
The only really bad thing is the name, as Florian already said, with the
versions in strutils and math.  The clash with delphi compatible functions
should be avoided.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread silvioprog
On Sun, Jan 31, 2016 at 11:43 AM, Sven Barth 
wrote:

> Hello together!
>
> I've finally come around to add a feature that many people have asked
> for throughout the years: an inline if that works like the if-statement
> in that it only evaluates that expression that is indeed returned.
>
> After the discussion last year about the inline-if I've decided to add
> it as an intrinsic function instead of an extension of the language.
> Like all intrinsics it's part of the System unit and "declared" like this:
>
> === code begin ===
>
> function IfThen(Condition: Boolean; ThenExpr, ElseExpr: type): type;
>
> === code end ===
>
[...]

> If there are any combinations of types that should work, but do not,
> please don't hesistate to report them at http://bugs.freepascal.org/


First issue sent to: http://bugs.freepascal.org/view.php?id=29554. :-)

-- 
Silvio Clécio
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread silvioprog
On Sun, Jan 31, 2016 at 1:03 PM, silvioprog  wrote:
[...]
>
> If there are any combinations of types that should work, but do not,
>> please don't hesistate to report them at http://bugs.freepascal.org/
>
>
> First issue sent to: http://bugs.freepascal.org/view.php?id=29554. :-)
>

And: http://bugs.freepascal.org/view.php?id=29555.

Sorry for fast reporting! ^^'

-- 
Silvio Clécio
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread Sven Barth
Hello together!

I've finally come around to add a feature that many people have asked
for throughout the years: an inline if that works like the if-statement
in that it only evaluates that expression that is indeed returned.

After the discussion last year about the inline-if I've decided to add
it as an intrinsic function instead of an extension of the language.
Like all intrinsics it's part of the System unit and "declared" like this:

=== code begin ===

function IfThen(Condition: Boolean; ThenExpr, ElseExpr: type): type;

=== code end ===

Since it's declared in the System unit it won't interfere with the
IfThen()s that are declared in the Math unit or other units, so they'll
continue to work as before to avoid any surprises.

An important point to note is that in general the result type is
determined by the ThenExpr (there are a few usability exceptions
regarding strings and chars).

Examples:

=== code begin ===

function A: LongInt;
begin
  A := 42;
end;

function B: LongInt;
  B := 21;
end;

var
  i, j: LongInt;
  s: String;
begin
  i := 42;
  j ;= IfThen(i < 32, 48, 21);

  j := IfThen(True, 23, 49); // compiler will warn of unreachable code
  j := IfThen(False, 23, 49); // compiler will warn of unreachable code

  j := IfThen(i < 32, A, B); // in this case only B will be executed

  //j := IfThen(i < 32, 32, '123'); // this will fail if there's no
suitable conversion operator available

  //s := IfThen(j < 32, #42, 'Foo'); // this will fail as a char is expected
  s := IfThen(j < 32, #42, #3294); // this will work however and will
result in a WideChar
  s := IfThen(j < 32, 'Hello World', 'Hello'#3294'World'); // this will
also work and will result in a WideString (and a warning as s is a
Short-/AnsiString)
end;

=== code end ===

If there are any combinations of types that should work, but do not,
please don't hesistate to report them at http://bugs.freepascal.org/

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread silvioprog
On Sun, Jan 31, 2016 at 11:43 AM, Sven Barth 
wrote:

> Hello together!
>
> I've finally come around to add a feature that many people have asked
> for throughout the years: an inline if that works like the if-statement
> in that it only evaluates that expression that is indeed returned.
>
> After the discussion last year about the inline-if I've decided to add
> it as an intrinsic function instead of an extension of the language.
> Like all intrinsics it's part of the System unit and "declared" like this:
>
> === code begin ===
>
> function IfThen(Condition: Boolean; ThenExpr, ElseExpr: type): type;
>
> === code end ===
>
> Since it's declared in the System unit it won't interfere with the
> IfThen()s that are declared in the Math unit or other units, so they'll
> continue to work as before to avoid any surprises.
>
> An important point to note is that in general the result type is
> determined by the ThenExpr (there are a few usability exceptions
> regarding strings and chars).
>
> Examples:
>
> === code begin ===
>
> function A: LongInt;
> begin
>   A := 42;
> end;
>
> function B: LongInt;
>   B := 21;
> end;
>
> var
>   i, j: LongInt;
>   s: String;
> begin
>   i := 42;
>   j ;= IfThen(i < 32, 48, 21);
>
>   j := IfThen(True, 23, 49); // compiler will warn of unreachable code
>   j := IfThen(False, 23, 49); // compiler will warn of unreachable code
>
>   j := IfThen(i < 32, A, B); // in this case only B will be executed
>
>   //j := IfThen(i < 32, 32, '123'); // this will fail if there's no
> suitable conversion operator available
>
>   //s := IfThen(j < 32, #42, 'Foo'); // this will fail as a char is
> expected
>   s := IfThen(j < 32, #42, #3294); // this will work however and will
> result in a WideChar
>   s := IfThen(j < 32, 'Hello World', 'Hello'#3294'World'); // this will
> also work and will result in a WideString (and a warning as s is a
> Short-/AnsiString)
> end;
>
> === code end ===
>
> If there are any combinations of types that should work, but do not,
> please don't hesistate to report them at http://bugs.freepascal.org/
>

Wooow! I did a request about it many years ago, however it was rejected
because the limitation in the generic feature at that time.

So, I posted a bug related to it yesterday:

http://bugs.freepascal.org/view.php?id=29546

Can I use it after this new feature? (I'm upgrading my FPC sources now :-) )

Buddy, thanks a lot for that feature, it is very useful! o/

-- 
Silvio Clécio
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] New feature: IfThen() intrinsic

2016-01-31 Thread silvioprog
On Sun, Jan 31, 2016 at 12:20 PM, Sven Barth 
wrote:

> On 31.01.2016 16:09, silvioprog wrote:
> > Wooow! I did a request about it many years ago, however it was rejected
> > because the limitation in the generic feature at that time.
>
> This implementation has nothing to do with generics. It's a compiler
> magic function, just like Writeln() and thus it can bend the usual rules ;)


Awesome.

> So, I posted a bug related to it yesterday:
> >
> > http://bugs.freepascal.org/view.php?id=29546


I have a test-case in a draft project that worked fine some days ago, so I
think that this bug was introduced recently. o_O

I've seen your bug this night and that was the cause for finally
> implementing this.


2^64 thanks! :-)

> Can I use it after this new feature? (I'm upgrading my FPC sources now
> :-) )
>
> I've not yet looked at what the problem regarding your code is. However
> you can simply remove your TUtils.Iif() and use IfThen() instead.


YES! o/ .. \o .. \o/

I'm going to do it now ... :-D

> Buddy, thanks a lot for that feature, it is very useful! o/
>
> That's what I have hoped for. :)


^^

-- 
Silvio Clécio
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal