Recent

Author Topic: Conditional ternary operators For FreePascal?  (Read 5320 times)

my_only_lonely

  • New Member
  • *
  • Posts: 16
Conditional ternary operators For FreePascal?
« on: August 04, 2025, 05:03:49 pm »
This thread was edited by Moderation, due to some content violating 3rd party rights
Several messages had to be edited, a few were removed. We apologies to anyone who contributed and now had their content altered.
Please see our forum rules, and respect the rights of others




Delphi 13.0 adds a conditional ternary operator operators:

https://blogs.embarcadero.com/coming-in-rad-studio-13-a-conditional-ternary-operator-for-the-delphi-language/

Will FreePascal consider supporting them? This could be really usefull.

Code: Pascal  [Select][+][-]
  1. X := if Left < 100 then 22 else 45;
« Last Edit: August 08, 2025, 09:49:06 am by Martin_fr »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Any really useful new syntax in Delphi?  Seems it is all shorthand and copycatting nowadays.

my_only_lonely

  • New Member
  • *
  • Posts: 16
Any really useful new syntax in Delphi?  Seems it is all shorthand and copycatting nowadays.

But they are highly requested by many people long time ago, at least by Delphi users. Maybe FreePascal targets different sets of developers.
You could say they are all short handed, but many modern languages, even C++ is adding more syntax sugar to the language.
« Last Edit: August 04, 2025, 05:32:43 pm by my_only_lonely »

d2010

  • Sr. Member
  • ****
  • Posts: 264
Delphi 13.0 adds a conditional ternary operator:
https://blogs.embarcadero.com/coming-in-rad-studio-13-a-conditional-ternary-operator-for-the-delphi-language/

Will FreePascal consider supporting them? This could be really usefull.
Please , please,please  No, Not , or Niet , I give the  BOR (Bool+ Or) you read line13+line14  after C:Q1=
Please No, niet, nu,.no , Id_no.


Code: Pascal  [Select][+][-]
  1. X := if Left < 100 then 22 else 45;

I believe, modify too deep of Lazarus-Parser,  is worst, and perhaps they lose too much  time.

I believe, the Team-of-Lazarus
  modify too deeply of Lazarus-token,  is worst, and perhaps they lose too much  time.
 :-X

Code: Pascal  [Select][+][-]
  1. Var iforg:array[00..31] of pointer;
  2.      x:integer=00;
  3.      yourage:longint=43;
  4.  idx_ififi:word=00;
  5. function ifif(index:word):boolean;
  6. begin idx_ififi:=index and 15;
  7.          result:=true;
  8. End;  
  9.  
  10. function ifif(index:word;checkit:boolean):boolean;overload;
  11. begin idx_ififi:=index and 15;
  12.          @BOR:=checkit;
  13.  // operator BOR have variabile ADDRESS  or allocate 4-bytes of memory.
  14.  // in system.pas
  15.          result:=true;
  16. End;  
  17.  
  18.  ;)
  19.  
  20. Function inthen (value:integer):boolean;
  21. Begin  result:=true;
  22.          if iforg=nil  then else begin iforg[idx_ififi]:=value;result:=false;
  23.                             // I put reverse because  I need stop ,forced too stop the the BOR
  24.         end;
  25. End;
  26.  
  27. Function inelse (value:integer):boolean;
  28. Begin  
  29.         result:=false;
  30.          if iforg=nil  then RaiseError('Here is crash');
  31.          iforg[ idx_ififi]:=value;
  32.          result:=true;
  33. End;
  34.  
  35.  
  36. Begin  (* main *)
  37. x:=-001;
  38. fillchar(iforg,sizeof(iforg),00);
  39. iforg[0]:=@yourage;
  40. iforg.id[0]:=@X;
  41. yourage:=ifif(0) BOR Left<100 BOR inthen(22) BOR inelse(45);
  42. x:=ifif(1) BOR Right>102 BOR inthen(12)  BOR inelse (34);
  43.  

I believe, modify too deep of Lazarus-Tokenizer,  is worst worst, worst  and perhaps they lose too much  time.

C:Q1=How much , can overload the inelse , and inthen?
Code: Pascal  [Select][+][-]
  1. function ifst(index:word;checkit:boolean):boolean;overload;
  2. begin idx_ififi:=15+(index and 15);
  3.          @BOR:=checkit;
  4.  // operator BOR have variabile ADDRESS  or allocate 4-bytes of memory.
  5.  // in system.pas
  6.          result:=true;
  7. End;  
  8.  
  9.  Var astr:string='';
  10.       bstr:string='';
  11. Begin
  12.   iforg[0]:=@astr+4;
  13.   bstr:=ifst(000,Left<100) BOR inthenelse('Lazarus','Delphi3');
  14.   cstr:=ifst(017,Left>100) BOR inthenelse('Viceversa','Delphi7');
  15. .....
  16. End;
  17.  
  18. Function inelse (value:string):boolean;overload;
  19. Function inthen (value:string):boolean;overload;
  20. Function inelse (value:double):boolean;overload;
  21. Function inthen (value:double):boolean;overload;
  22.  

Best regards.
« Last Edit: August 06, 2025, 02:44:29 pm by Martin_fr »

Warfley

  • Hero Member
  • *****
  • Posts: 2057
Any really useful new syntax in Delphi?  Seems it is all shorthand and copycatting nowadays.
Well this is not just syntactic sugar, it allows you to do the following:
Code: Pascal  [Select][+][-]
  1. const a = if cond then val1 else val2;
A constant can only be assigned exactly once, so the following is not possible:
Code: Pascal  [Select][+][-]
  1. const a;
  2. if cond then a:=val1 else a:=val2;

So this is not just syntactic sugar, it allows you to use complex expressions on constants that was not possible before.

Bart

  • Hero Member
  • *****
  • Posts: 5720
    • Bart en Mariska's Webstek
Well, why not just change Pascal into C or whatever  >:D

Warfley

  • Hero Member
  • *****
  • Posts: 2057
Well, why not just change Pascal into C or whatever  >:D
All languages learn from each other and develop. If you refuse to take good and proven concepts from other languages just because you don't like those other languages, your language will just end up worse.

Look at the C standard, C itself takes a lot of new features from other languages. For example since 2023 C now supports enum types without an underlying ordinal type, something that first was introduced in Pascal. It also now supports binary representation of literals like 0b101 which is the same as %101 in Pascal. Or the use of keywords for "true" and "false" instead of custom defined constants.
Just to name 3 features that Pascal has for a while which were newly added to C

my_only_lonely

  • New Member
  • *
  • Posts: 16
Well, why not just change Pascal into C or whatever  >:D
All languages learn from each other and develop. If you refuse to take good and proven concepts from other languages just because you don't like those other languages, your language will just end up worse.

Look at the C standard, C itself takes a lot of new features from other languages. For example since 2023 C now supports enum types without an underlying ordinal type, something that first was introduced in Pascal. It also now supports binary representation of literals like 0b101 which is the same as %101 in Pascal. Or the use of keywords for "true" and "false" instead of custom defined constants.
Just to name 3 features that Pascal has for a while which were newly added to C
Good point!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
But there must be a selection process. It can't be "copy all stuff from all other languages".

So the first step must be the existence of an opposing party. And the stronger the opposition the better. That ensure that if it then gets through, some value is very likely in it.

There also must be an "open state" at the beginning. That is, rejection must be a possibility.


Also, keep in mind that fpc trunk has many new features. And some of them surely existed outside Pascal before... So it is definitely happening.



concepts from other languages

I would say a "concept" must be something that is more than just a syntax.

E.g. "for (a =1 ; a< 2 ; a = a +1)" <= that is a syntax.

Sure the lines between concept and syntax are not always very clear, and many existing concepts could be broken down into underlaying concepts, making then essentially a syntax. But usually they have some threshold...

Pascal is biased towards verbosity.  And IMHO that is a good thing.  So, if something is considered that may be syntax, then that should not be something that takes away of that verbosity.

« Last Edit: August 06, 2025, 02:45:34 pm by Martin_fr »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Hey Martin,

Pascal is biased towards verbosity.  And IMHO that is a good thing.  So, if something is considered that may be syntax, then that should not be something that takes away of that verbosity.

On this we are in total agreement !!!

Cheers,
Gus

Warfley

  • Hero Member
  • *****
  • Posts: 2057
Ok first, I must be pedantic (sorry 😅)
E.g. "for (a =1 ; a< 2 ; a = a +1)" <= that is a syntax.
I'd disagree thats far more than a syntax. It's much more powerful than either a pascal for loop. Take the following:
Code: C  [Select][+][-]
  1. for (int cnt=0;(cnt=fread(fin, &buff, sizeof(buff))) > 0; fwrite(fout, &buff, cnt) {
  2.   printf("Processing %d bytes\n", cnt);
  3. }
If you want something like that, is a different question, but the C for syntax is immensly powerful. In the past I have written a complete Base64 decoder in a single for loop without a body. Am I proud of it? Yes, would I use it productively? Hell no xD

Also it's even more than just syntactic sugar around while, because take the following:
Code: C  [Select][+][-]
  1. for (prep;cond;step) {
  2.   body;
  3. }
Naively one could say it's basically just a wrapper around:
Code: C  [Select][+][-]
  1. prep;
  2. while (cond) {
  3.   body;
  4.   step;
  5. }
But it's not because step is also executed even if you do a "continue". So it's much more than just syntactic sugar.

Btw. this is also the reason why the FPC for loop is more than just syntactic sugar, because it enforces the step on continue, and also does bound checks to avoid overflow errors. So the following:
Code: Pascal  [Select][+][-]
  1. for i:=maxint-3 to MaxInt do
Is not equivalent to:
Code: C  [Select][+][-]
  1. for (int i=maxint-3; i<=maxint; ++i)
because the C code is due to overflow an infinite loop while the first one only does 3 steps.


But now about the main topic
Quote
Is shortening more than syntax?
Well it's part of a concept, it's called expression based programming and the underlying assumption is that if the programmer writes single expressions the code is simpler and less likely to devovle into spaghetti code, because you enforce a form of locality.

Often this is combined with the concept of constness. So for example, if you write:
Code: Pascal  [Select][+][-]
  1. const c = if cond then Value1 else Value2;
You have the value of c local to it's definition, and because it's const you cannot change it.
This means if you want to know what C is, you just need to look in one place, it's definition.

On the other hand if you write the following:
Code: Pascal  [Select][+][-]
  1. var c;
  2. ...
  3. if cond then
  4.   c := Value1
  5. else
  6.   c := Value2;
You now spread the same information over multiple lines with multiple statements. Simultaneously to allow this C must be a variable and can't be a constant, so you can e.g. through a typo, accidentally assign a new value to C, which if it were a constant would throw a compiler error.

If your language supports and enforces constness, complex expressions are necessary to utilize this feature.
FreePascal does not support this, because it would require inline const definitions, which the core development team is heavily against, so it is not that much of an issue, but for example in C where constness is baked into the typesystem, a ternary if operator makes much more sense.

Lastly this also often comes with the assumptions that it's better to expressly write down your conditions each line, instead of having one big block. E.g.
Code: Pascal  [Select][+][-]
  1. const c1 = if cond then Value1 else Value2;
  2. const c2 = if cond then Value3 else Value4;
  3. // Instead of
  4. var c1, c2;
  5. ...
  6. if cond then
  7. begin
  8.   c1:=Value1;
  9.   c2:=Value3;
  10. end
  11. else
  12. begin
  13.   c1:=Value2;
  14.   c2:=Value4;
  15. end;
The idea is that the former makes it much easier to see what is happening logically while the second example mixes c1 and c2 together just because they share the same condition, even though their logic may be semantically completely unrealated.


« Last Edit: August 06, 2025, 02:48:37 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
Ok first, I must be pedantic (sorry 😅)

True, bad example. But not the point. I take it, the message of that part was still clear: if you use this with the same limits as the Pascal for loop, then it is syntax.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
Well it's part of a concept, it's called expression based programming and the underlying assumption is that if the programmer writes single expressions the code is simpler and less likely to devovle into spaghetti code, because you enforce a form of locality.

As for the underlying assumption, I have seen a lot of code that then has expressions wrapping multiple lines (several dozen in some cases).
And a long list of "if" is still easier to read, than a single non-ending expression. (At least they are to me).

But anyway, yes "expression based" is a concept. (On its own not a good one / Please see comment after next quote to put this answer in context)

Quote

Often this is combined with the concept of constness. So for example, if you write:
Code: Pascal  [Select][+][-]
  1. const c = if cond then Value1 else Value2;
You have the value of c local to it's definition, and because it's const you cannot change it.
This means if you want to know what C is, you just need to look in one place, it's definition.

Is this really about "const"? Or is this (also) about immutable values? The difference to "const" being that the values are not known at compile time, and also can differ on each invocation of a function (or the context).

Immutability can be great. And can balance the negative cost of (potentially) really long expressions.

However, while it can be helped by more flexible expressions, it does not strictly need them.



EDIT: Or maybe in C "const" stand for immutable, not for a constant (aka compile time constant)?

Quote


On the other hand if you write the following:
Code: Pascal  [Select][+][-]
  1. var c;
  2. ...
  3. if cond then
  4.   c := Value1
  5. else
  6.   c := Value2;
You now spread the same information over multiple lines with multiple statements. Simultaneously to allow this C must be a variable and can't be a constant, so you can e.g. through a typo, accidentally assign a new value to C, which if it were a constant would throw a compiler error.
Sounds more like immutable than constants?



Quote
Lastly this also often comes with the assumptions that it's better to expressly write down your conditions each line, instead of having one big block. E.g.
Code: Pascal  [Select][+][-]
  1. const c1 = if cond then Value1 else Value2;
  2. const c2 = if cond then Value3 else Value4;
  3. // Instead of
  4. var c1, c2;
  5. ...
  6. if cond then
  7. begin
  8.   c1:=Value1;
  9.   c2:=Value3;
  10. end
  11. else
  12. begin
  13.   c1:=Value2;
  14.   c2:=Value4;
  15. end;
The idea is that the former makes it much easier to see what is happening logically while the second example mixes c1 and c2 together just because they share the same condition, even though their logic may be semantically completely unrealated.

Unrelated to the actual topic:

As you say "their logic may be semantically completely unrealated" => this is not a question of feature or syntax, but of correctly expressing what your code does.

- If they are related, then the "if blocks" of your example are fine, and good and better than the expression.
- If they are not related the ":= if" is one way (and I have no objection to it), but for that example case another way exists already:
Code: Pascal  [Select][+][-]
  1.   if cond then c1 := Value1 else c1 := Value2;
  2.   if cond then c2 := Value1 else c2 := Value2;
  3.  
Same as clear.

Please mind, I did deliberately ignore the "const" part
- that has been addressed further up
- that has nothing to do with the statement "their logic may be semantically completely unrealated"



« Last Edit: August 06, 2025, 02:53:28 pm by Martin_fr »

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
If there is something in Delphi, then it should be brought to FreePascal as early as possible

With Delphi's Syntax it is not about what the Syntax is

The point of Delphi  Syntax is that you can use a library  programmed for Delphi in FreePascal  without having to rewrite everything of the library 

EganSolo

  • Sr. Member
  • ****
  • Posts: 398
My $0.02:

From its inception, Pascal adhered to a mathematically sound model and favored later readability over first-time code productivity.

If you want the latter, use Perl or PhP or any of these derivatives. C is now middle-of-the-road.

The idea that a constant can be dynamically assigned a value is, well, a fallacy. By definition, a constant is constant. It does not change based on context or conditions. Therefore, that concession on the current proprietors of Delphi is most likely chasing the .NET shadow or whatever language occupies the latest fad.

With Pascal, less is more.

 

TinyPortal © 2005-2018