Conditional ternary operators For FreePascal? lazarus / 62-General ---------------------------------------------------------------------- my_only_lonely · 2025-08-04 15:03 This thread was edited by Moderation, due to some content violating 3rd party rightsSeveral 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i X := if Left < 100 then 22 else 45; ---------------------------------------------------------------------- marcov · 2025-08-04 15:18 Any really useful new syntax in Delphi?  Seems it is all shorthand and copycatting nowadays. ---------------------------------------------------------------------- my_only_lonely · 2025-08-04 15:30 Quote from: marcov on August 04, 2025, 05:18:03 pmAny 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. ---------------------------------------------------------------------- d2010 · 2025-08-04 15:38 Quote from: my_only_lonely on August 04, 2025, 05:03:49 pmDelphi 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var iforg:array[00..31] of pointer;     x:integer=00;     yourage:longint=43; idx_ififi:word=00;function ifif(index:word):boolean;begin idx_ififi:=index and 15;         result:=true;End;    function ifif(index:word;checkit:boolean):boolean;overload;begin idx_ififi:=index and 15;         @BOR:=checkit;  // operator BOR have variabile ADDRESS  or allocate 4-bytes of memory. // in system.pas       result:=true;End;     ;) Function inthen (value:integer):boolean;Begin  result:=true;         if iforg=nil  then else begin iforg[idx_ififi]:=value;result:=false;                          // I put reverse because  I need stop ,forced too stop the the BOR        end;End; Function inelse (value:integer):boolean;Begin       result:=false;         if iforg=nil  then RaiseError('Here is crash');         iforg[ idx_ififi]:=value;         result:=true;End;  Begin  (* main *)x:=- 001;fillchar(iforg,sizeof(iforg),00);iforg[0]:=@yourage;iforg.id[0]:=@ X;yourage:=ifif(0) BOR Left<100 BOR inthen(22) BOR inelse(45);x:=ifif(1) BOR Right>102 BOR inthen(12)  BOR inelse (34);  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ifst(index:word;checkit:boolean):boolean;overload;begin idx_ififi:=15+(index and 15);         @BOR:=checkit;  // operator BOR have variabile ADDRESS  or allocate 4-bytes of memory. // in system.pas         result:=true;End;     Var astr:string='';      bstr:string='';Begin   iforg[0]:=@astr+4;  bstr:=ifst(000,Left<100) BOR inthenelse('Lazarus','Delphi3');  cstr:=ifst(017,Left>100) BOR inthenelse('Viceversa','Delphi7');..... End; Function inelse (value:string):boolean;overload;Function inthen (value:string):boolean;overload;Function inelse (value:double):boolean;overload;Function inthen (value:double):boolean;overload;  Best regards. ---------------------------------------------------------------------- Warfley · 2025-08-04 16:07 Quote from: marcov on August 04, 2025, 05:18:03 pmAny 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const a;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 · 2025-08-04 16:12 Well, why not just change Pascal into C or whatever ---------------------------------------------------------------------- Warfley · 2025-08-04 16:28 Quote from: Bart on August 04, 2025, 06:12:51 pmWell, why not just change Pascal into C or whatever  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 · 2025-08-04 17:40 Quote from: Warfley on August 04, 2025, 06:28:01 pmQuote from: Bart on August 04, 2025, 06:12:51 pmWell, why not just change Pascal into C or whatever  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 · 2025-08-04 18:08 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. Quote from: Warfley on August 04, 2025, 06:28:01 pm 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. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2025-08-04 18:12 Hey Martin, Quote from: Martin_fr on August 04, 2025, 08:08:59 pmPascal 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 · 2025-08-04 19:24 Ok first, I must be pedantic (sorry 😅) Quote from: Martin_fr on August 04, 2025, 08:08:59 pmE.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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for (int cnt=0;(cnt=fread(fin, &buff, sizeof(buff))) > 0; fwrite(fout, &buff, cnt) {  printf("Processing %d bytes\n", cnt);}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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for (prep;cond;step) {  body;}Naively one could say it's basically just a wrapper around: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i prep;while (cond) {  body;  step;}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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=maxint-3 to MaxInt doIs not equivalent to: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 QuoteIs 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var c;...if cond then  c := Value1else  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const c1 = if cond then Value1 else Value2;const c2 = if cond then Value3 else Value4;// Instead ofvar c1, c2;...if cond thenbegin  c1:=Value1;  c2:=Value3;endelsebegin  c1:=Value2;  c2:=Value4;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. ---------------------------------------------------------------------- Martin_fr · 2025-08-04 19:39 Quote from: Warfley on August 04, 2025, 09:24:12 pmOk 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 · 2025-08-04 20:09 Quote from: Warfley on August 04, 2025, 09:24:12 pmWell 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var c;...if cond then  c := Value1else  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? QuoteLastly 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const c1 = if cond then Value1 else Value2;const c2 = if cond then Value3 else Value4;// Instead ofvar c1, c2;...if cond thenbegin  c1:=Value1;  c2:=Value3;endelsebegin  c1:=Value2;  c2:=Value4;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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if cond then c1 := Value1 else c1 := Value2;  if cond then c2 := Value1 else c2 := Value2; 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" ---------------------------------------------------------------------- BeniBela · 2025-08-04 20:27 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 · 2025-08-05 01:20 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. ---------------------------------------------------------------------- marcov · 2025-08-05 07:03 Quote from: my_only_lonely on August 04, 2025, 05:30:52 pmQuote from: marcov on August 04, 2025, 05:18:03 pmAny 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. It is not the developers of Delphi that decide the features, but marketing. QuoteYou could say they are all short handed, but many modern languages, even C++ is adding more syntax sugar to the language. I can't speak for C++, but in Delphi's case it is obviously riskless superficial features to keep the subscription train going. ---------------------------------------------------------------------- marcov · 2025-08-05 08:36 Quote from: Warfley on August 04, 2025, 06:07:36 pmSo this is not just syntactic sugar, it allows you to use complex expressions on constants that was not possible before. And you think THAT is the reason it was added ? ---------------------------------------------------------------------- nanobit · 2025-08-05 14:47 Quote from: Warfley on August 04, 2025, 06:28:01 pmLook 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. Wrong. C enum has an underlying ordinal type: default type or declared type (newer feature). ---------------------------------------------------------------------- Martin_fr · 2025-08-08 07:33 This thread is moderated. Please see the note on top (in the first message of this thread) ---------------------------------------------------------------------- gues1 · 2025-08-08 13:47 Quote from: Warfley on August 04, 2025, 06:07:36 pmWell this is not just syntactic sugar, it allows you to do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const a;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. Quote from: EganSolo on August 05, 2025, 03:20:53 amThe 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. Who said that in Delphi you can write a thing like that (I mean const c := if ....) ? From what I know, this certainly isn't possible in the normal variable declaration sections (interface, implementation, and as local variable). The const declaration must be known at compiler time, not at runtime, and the "if" statement already contradicts this. If we then talk about the concept of inline variables (which isn't part of FPC), then perhaps this will be possible because it can be resolved at runtime. But will see when Delphi 13 Florence wil be out. NEWS note: this is known in the public e-mail lists from authorized channel so no issue to talk about, but not published in the Embarcadero blog yet, there will be a free Webinar on 10 September about the new features and the realease of new Rad Studio. ---------------------------------------------------------------------- PascalDragon · 2025-08-08 20:08 Quote from: BeniBela on August 04, 2025, 10:27:31 pmIf there is something in Delphi, then it should be brought to FreePascal as early as possible No. There can be features where we feel that the spirit of Pascal is violated too much (inline variables) or the implementations are simply not trivial (implements on a field of a class type, dynamic packages, delayed imports).