In Delphi 13 Embarcadero introduced a new syntax construct for inline if expressions which they call “ternary operator“. It implements what previously had to be implemented with the overloaded IfThen functions as a native compiler feature.
These look like this.
Variable := if Condition then TrueValue else FalseValue;
At the same time they removed the IDE’s built in code formatter, so as far as I know there was no code formatter that supports this construct. Until now that is. As of revision #4993 the GExperts Code Formatter supports it.
The current implementation keeps ternary operators on one line and handles them correctly in all contexts (constants, function parameters, case statements, loop bounds, etc.). The plan is to add a configuration option for vertical alignment of the then and else keywords.
The implementation was done using Claude Code. I did not write a single line of code for it. I only gave the “AI” a description of the new language construct, told it to look up additional information on the internet, and suggest how to format it. I selected the simple format to use. I then had it create test cases and implement the new feature. It made several mistakes in the process, in particular it wrote wrong test cases but with me overseeing the process whenever I had to take a break from my actual work and telling it where it erred, it wrote the whole code itself. After finally all unit tests succeeded again, including the new one for the inline if expression, I committed the changes and here you are: Support for the “ternary operator”.
There is no new GExperts release for this feature. If you want to use it, you must compile your own DLL.
Discussion about this blog post (link to be added later, because the forum is very slow at the moment) in the international DelphiPraxis forum.