Sign in Join

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

Mailing lists / fpc-devel / 2011-01

Proposal: IIF - IfThen Cond True False

12 messages · 7 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I come here humbly to ask for a new implementation, the IIF ?:, the IIF is a shortcut that make the coding a little faster, it works by returning a true or a false part of a condition, a more detailed information can be found there: http://en.wikipedia.org/wiki/%3F:

*PHP and C++ implementation:* Value *=* condition *?* value if true *:*
value if false;
*Pascal implementation:* Value := IfThen(Condition, TruePart, FalsePart);

*function IfThen(Condition: Boolean; IfTrue, IfFalse: X): X; overload; begin

     if Condition then Result := IfTrue else Result := IfFalse;

end;
*
The problem: we have to do *one function for every type*, on the C and PHP method is is *globally*
If we want one for Integer we must declare one for it, if we want one for X we need to declare it...

I want to know from you, what you would think about it on FPC? I don't belive that it is a hard feature to implement and is helpfull. I'll be waiting for your response!

*If this is not the right mailing list for proposals, then I apologize. *
Thank you,
Bizz

Reply by email

Paul Ishenin
to Unknown author
Show quoted text 8 lines
The problem: we have to do *one function for every type*, on the C and PHP method is is *globally* If we want one for Integer we must declare one for it, if we want one for X we need to declare it... I want to know from you, what you would think about it on FPC? I don't belive that it is a hard feature to implement and is helpfull. I'll be waiting for your response!
Similar to delphi generic method:
  function IIf<T>(Condition: Boolean; IfTrue, IfFalse: T): T;
  begin
    if Condition then
      Result := IfTrue
    else
      Result := IfFalse;
  end;

I'm working on this functionality for classes, objects and record methods.

But even delphi does not have it for regular procedures/functions. I suppose because regular procedures/functions does not support overloading.

Best regards,
Paul Ishenin

Reply by email

Marco van de Voort
to Paul Ishenin

In our previous episode, Paul Ishenin said:

But even delphi does not have it for regular procedures/functions. I suppose because regular procedures/functions does not support overloading.

??? I use it all the time? Requires overload directive iirc, but works.

Reply by email

Paul Ishenin
to Marco van de Voort

12.01.2011 13:47, Marco van de Voort пишет:

Show quoted text 6 lines
In our previous episode, Paul Ishenin said: > But even delphi does not have it for regular procedures/functions. I > suppose because regular procedures/functions does not support overloading. ??? I use it all the time? Requires overload directive iirc, but works.

Indeed... I somehow have misled myself.

Well, then I don't know the reason why delphi does not have generic functions/procedures.

Best regards,
Paul Ishenin

Reply by email

Unknown author
to Marco van de Voort: In our previous episode, Paul Ishenin said: ??? I use it all the time? Requires overload…
Marco van de Voort wrote 8 lines
In our previous episode, Paul Ishenin said: > But even delphi does not have it for regular procedures/functions. I > suppose because regular procedures/functions does not support overloading. ??? I use it all the time? Requires overload directive iirc, but works. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Well guys, thanks for replying, the generic method is a very nice method, I personally didn't know that the FPC support it, very thank you for that!

Reply by email

Paul Ishenin
to Unknown author

12.01.2011 13:54, пишет:

Well guys, thanks for replying, the generic method is a very nice method, I personally didn't know that the FPC support it, very thank you for that!

FPC does not support it at the moment. I hope it will in a month.

Best regards,
Paul Ishenin

Reply by email

Unknown author
to Paul Ishenin
Paul Ishenin wrote 13 lines
12.01.2011 13:54, пишет: > Well guys, thanks for replying, the generic method is a very nice > method, I personally didn't know that the FPC support it, very thank you > for that! FPC does not support it at the moment. I hope it will in a month. Best regards, Paul Ishenin _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Ok, I'll be waiting for that!

Guys, you reply fast and nice!

Thank you again, anyways =)

Reply by email

Sven Barth
to Paul Ishenin: 12.01.2011 13:54, пишет: FPC does not support it at the moment. I hope it will in a…
Paul Ishenin wrote 6 lines
12.01.2011 13:54, пишет: > Well guys, thanks for replying, the generic method is a very nice > method, I personally didn't know that the FPC support it, very thank you > for that! FPC does not support it at the moment. I hope it will in a month.

Oh wow O.o

That's why I love Free Pascal...

Btw: Do you plan to implement constraints as well?

Regards,
Sven

Reply by email

Hans-Peter Diettrich
to Unknown author: I come here humbly to ask for a new implementation, the IIF ?:, the IIF is a shortcut…
Show quoted text 8 lines
I come here humbly to ask for a new implementation, the IIF ?:, the IIF is a shortcut that make the coding a little faster, it works by returning a true or a false part of a condition, a more detailed information can be found there: http://en.wikipedia.org/wiki/%3F: *PHP and C++ implementation:* Value *=* condition *?* value if true *:* value if false; *Pascal implementation:* Value := IfThen(Condition, TruePart, FalsePart);
The standard Pascal implementation of the C ternary expression is a
   const Name: array[boolean] of <type> = (FalseFart, TruePart);
and
   Value := Name[Condition];

DoDi

Reply by email

Marc Weustink
to Unknown author: I come here humbly to ask for a new implementation, the IIF ?:, the IIF is a shortcut…
Show quoted text 14 lines
I come here humbly to ask for a new implementation, the IIF ?:, the IIF is a shortcut that make the coding a little faster, it works by returning a true or a false part of a condition, a more detailed information can be found there: http://en.wikipedia.org/wiki/%3F: *PHP and C++ implementation:* Value *=* condition *?* value if true *:* value if false; *Pascal implementation:* Value := IfThen(Condition, TruePart, FalsePart); *function IfThen(Condition: Boolean; IfTrue, IfFalse: X): X; overload; begin if Condition then Result := IfTrue else Result := IfFalse; end; *

Note that there is a tiny little difference between those two.

the ?: operator evaluates either the TruePart or FalsePart while IfThen will evaluate both parts first.

The following will succeed

var
   p: PInteger;
   i: Integer;
begin
   p := nil;
   i := p  nil ? p^ : 0;
end;

The following will fail

var
   p: PInteger;
   i: Integer;
begin
   p := nil;
   i := IfThen(p  nil, p^ , 0);
end;

Reply by email

Alexander Klenin
to Marc Weustink
Marc Weustink wrote 5 lines
Note that there is a tiny little difference between those two. the ?: operator evaluates either the TruePart or FalsePart while IfThen will evaluate both parts first.

Exactly. This is why for me IfThen is much less useful then conditional operators in other languages.
I think a better alternative would be Algol-like if-expression: a := (if x > 0 then x else -x);

Alexander S. Klenin

Reply by email