Recent

Author Topic: Q about new IfThen() intrinsic  (Read 20893 times)

Azarien

  • New member
  • *
  • Posts: 9
Re: Q about new IfThen() intrinsic
« Reply #30 on: March 08, 2016, 12:59:59 pm »
Ada 2012 introduced the following syntax:

Code: Pascal  [Select][+][-]
  1. z:=(if b then 5 else 42);

Note that the if-expression (that's what it is called) has to be parenthesized. This solves precedence problem.

This is also legal:

Code: Pascal  [Select][+][-]
  1. f:=float(if b then x else y);

which is equivalent to

Code: Pascal  [Select][+][-]
  1. f:=(if b then float(x) else float(y));

When the parentheses are required anyways, no need for additional ones:

Code: Pascal  [Select][+][-]
  1. func(if x then a else b);

I think Free Pascal could simply copy Ada syntax.
The rationale can be found here.
« Last Edit: March 08, 2016, 01:06:03 pm by Azarien »

Azarien

  • New member
  • *
  • Posts: 9
Re: Q about new IfThen() intrinsic
« Reply #31 on: March 08, 2016, 01:43:59 pm »
Some other Ada 2012 examples:
Code: Pascal  [Select][+][-]
  1. P(if X > 0 then B elsif X < 0 then C else D);
  2. // we should use "else if" here
  3.  
  4. P(if X > 0 then (if Y > 0 then B else C) else D);
  5.  
  6. // special case for boolean:
  7. P(if C1 then C2);
  8. // same as
  9. P(if C1 then C2 else True);

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12894
  • FPC developer.
Re: Q about new IfThen() intrinsic
« Reply #32 on: March 08, 2016, 03:12:30 pm »
Borrowing from Ada was already discussed in the relevant threads on the maillists.

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Q about new IfThen() intrinsic
« Reply #33 on: June 03, 2016, 12:41:23 am »
Why ifthen() while there is Case.

Code: Pascal  [Select][+][-]
  1. Case Variable of
  2.   true: // do something
  3.   false: // do something
  4. end
  5.  
.. and if CASE command doesn't allow comparisons why ifThen() doesn't use similar clear construct as case or extend the functionality of the Case Of command. (which is really nice and clear to read compared to that brainfuck IFTHEN(=?=?=?=_?= ).. Noob is wondering.

PS. If something like ifThen() is implemented why not name it to Choose() as what I can see it do just that chose the value based on the boolean comparison(or is test in programming jargon). ??

PPS.
..or
Code: Pascal  [Select][+][-]
  1. Case Variable_A of
  2.   true: // do something
  3.   false: // do something
  4.  To variable_B
  5. end
  6.  

..or even
Code: Pascal  [Select][+][-]
  1. To variable1, variable2, variable3, variable4
  2. begin
  3.   case Variable_A of
  4.      true: // do something
  5.      false: // do something
  6.   end
  7. end
  8.  

..or even as unorthodox as
Code: Pascal  [Select][+][-]
  1. If A<>B  Then ChooseFor variable1, variable2, variable3, variable4 (*ie. list of 50 GUI elements to disable*)
  2.     True: (* actions *)
  3.     False: (* actions *)
  4.  
« Last Edit: June 03, 2016, 01:53:33 am by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

 

TinyPortal © 2005-2018