Recent

Author Topic: functional IF  (Read 9782 times)

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: functional IF
« Reply #45 on: June 16, 2025, 12:53:17 pm »
I've been working on something similar for the SharpBASIC compiler, but instead of using IF in an expression, a LET statement is used allowing for one or more 'on' branches:

Code: Text  [Select][+][-]
  1. let s
  2.   on x < y is "Foo";
  3.   on x > y is "Bar" else "Nothing";
  4. end
  5. print(s);

It differs from a case/switch block in that it's purely an assignment statement. This construct clearly separates assignment from boolean evaluation.
This wouldn't work for multiple reasons, first, is is already defined as an operator, meaning it can't be used to follow up on an expression. Especially in this case where it would introduce additional ambiguity:
Code: Pascal  [Select][+][-]
  1. on MyObject = OtherObject is TMyClass
In this case OtherObject is TMyClass is a perfectly normal expression.
Second in Pascal you can't define variables in the code section meaning such a let construct is also not possible.
Lastly the FPC team does not like the introduction of new keywords, which means adding a new keyword for this is also not an option.

Looking at the keywords available, I don't really see a good syntax option here.

Aside from that, if it's only for assignment it's quite limiting, as one of the main usecases for this is using it in function parameters if you don't want to introduce a new variable. If you just do a grep for IfThen in the Lazarus sources, you'll find that around half the use cases of the current IfThen function is not as an assignment.

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: functional IF
« Reply #46 on: June 16, 2025, 01:54:44 pm »
Second in Pascal you can't define variables in the code section meaning such a let construct is also not possible.

In SharpBASIC definitions are also not allowed in the code section, similar to Pascal, so s must be defined first:

Code: Text  [Select][+][-]
  1. dim s:str;
  2. dim x, y:int;
  3. main do
  4.   let s
  5.     on x < y is "Foo";
  6.     on x > y is "Bar" else "Nothing";
  7.   end
  8.   print(s);
  9. end
  10.  

Like I said, it's an assignment, but I get the idea of having something like this implemented as an intrinsic.
It's only logical.

avk

  • Hero Member
  • *****
  • Posts: 826

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: functional IF
« Reply #48 on: July 31, 2025, 03:55:31 pm »
I understand from the comments on the bugtracker that a commit may be pending.....
https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/1037
The comments seem to suggest that it is by now more than a MR.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

bytebites

  • Hero Member
  • *****
  • Posts: 778
Re: functional IF
« Reply #49 on: July 31, 2025, 05:27:42 pm »
Pascaldragon once implemented it, but it was rejected. Sad for double work.

Principle, which does cause harm is not principle.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6359
  • Compiler Developer
Re: functional IF
« Reply #50 on: July 31, 2025, 09:17:32 pm »
Pascaldragon once implemented it, but it was rejected. Sad for double work.

Principle, which does cause harm is not principle.

Nowadays I'd implement as if <expr> then <expr> else <expr> instead of a magical IIF(<expr>, <expr>, <expr>) intrinsic (as I had done back then) anyway.

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: functional IF
« Reply #51 on: August 02, 2025, 12:28:58 am »
Still waiting for it!  ;D

Jamie

The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: functional IF
« Reply #52 on: August 02, 2025, 12:39:53 pm »
I liked the IIF() syntax more, but anything is better than yet another redundant syntax, so then I'm fine with functional if then else.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: functional IF
« Reply #53 on: August 02, 2025, 12:55:53 pm »
I liked the IIF() syntax more, but anything is better than yet another redundant syntax, so then I'm fine with functional if then else.

Speaking personally, I don't like conflating functions and control structures.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1344
  • Professional amateur ;-P
Re: functional IF
« Reply #54 on: August 04, 2025, 10:19:03 am »
Hey Y'All,

According to what Michael commented on the MR, since it's being adopted by Delphi, at least the if..then..else is being accepted.

I'm still unsure about the inclusion of the case and the try..except, for which I left a comment for Michael to clarify their inclusion. I'll let him voice his opinion on the MR.

But I would really like that all three would be merged, that's for sure !!

Cheers,
Gus

 

TinyPortal © 2005-2018