- Регистрация
- 9 Май 2015
- Сообщения
- 1,368
- Баллы
- 155
Embarcadero is working on a new version of Delphi (and RAD Studio) . In this blog post and others that are going to follow, we want to start introducing some of the exciting new features of this release. This blog post is based on a pre-release version of the RAD Studio software. No feature is committed until the product’s GA release.
Among other improvements to the Delphi language and its compilers, the RAD Studio 13 release will introduce a conditional operator (or ternary operator) to the Object Pascal language, for the first time. This has been one of the most requested language enhancements by customers for quite some time and this feature is available in the current Ganymede beta for testing.
Table of Contents
Delphi now has a Ternary Operator using a new form of the If keyword
A or conditional operator is an operator which behaves like an if statement, with a condition and two possible values. In many other programming languages, the ternary operator is indicated using the ?: syntax. In Delphi, we want to preserve as much as possible a familiar and Pascal-oriented syntax, so we decided to use the if symbol as an operator. In other words, if can now be used to indicate a statement or an operator, depending on the position in the source code.
This is an example of a simple assignment expression in two versions, the first based on a traditional if statement and the second on an if operator:
// classic if statement if Left < 100 then X := 22 else X := 45; // assignment with if operator X := if Left < 100 then 22 else 45;
The key difference is that the if operator can be used as part of any expression, as follows:
ShowMessage (if Left < 100 then 'Small' else 'Big')
Here you can see this code in the editor of the 64-bit version of the RAD Studio IDE, using the RAD Studio 13 beta.
The if operator in action in the RAD Studio 13 editor (in the 64-bit version of the IDE)
What are the compatibility rules of the Delphi Ternary Operator?
There are specific rules in terms of the coherence among the types of the two sub-expression. In short, they have to be of the same type, or a directly assignable type. If not you’ll get a compiler error, like in this case:
[dcc32 Error] Unit31.pas(38): E2010 Incompatible types: 'string' and 'Integer'
An example of an if operator incompatibility error
There is an extremely long table in the beta documentation with the different type combinations you can use in the new if expression, and the resulting type of the expression for each of these combinations. I’m not reporting it here as it’s a couple of pages long and goes to a level of detail not needed for this introduction.
What is the Order of Priority for the Delphi Ternary Operator?
When using the if-then-else operator within an expression, notice how other operations (e.g., addition) have higher priorities, which can lead to unexpected errors. Use extra parentheses, like you often have to do with low-priority Boolean operators. Let’s look at this code:
ShowMessage (if Left < 100 then 'Small' else 'Large' + 'Nice')
What’s going to be the output if Left is less than 100? It is “Small” or “SmallNice“? It’s the former, because the + has higher priority making it like:
ShowMessage (if Left < 100 then 'Small' else ('Large' + 'Nice'))
if you always want the additional word to be added, you should use:
ShowMessage ((if Left < 100 then 'Small' else 'Large') + 'Nice')
RAD Studio 13 will bring in a number of other new features and modernizations to the Delphi language
While this won’t be the only change to the Delphi language in RAD Studio 13, it is one of the most prominent and requested changes to the language we are going to introduce. There are other new operators coming along, and some new and expanded compiler directives. Overall Delphi 13 (part of RAD Studio 13) is going to be a very important release for the evolution of the Delphi Object Pascal language. Stay tuned.
This blog post is based on a pre-release version of the RAD Studio software. No feature is committed until the product’s GA release.
Why not and see why we think it’s the fastest, easiest, and most efficient way to create cross platform apps that work on Windows, macOS, Linux, iOS and Android from a single code base?
Among other improvements to the Delphi language and its compilers, the RAD Studio 13 release will introduce a conditional operator (or ternary operator) to the Object Pascal language, for the first time. This has been one of the most requested language enhancements by customers for quite some time and this feature is available in the current Ganymede beta for testing.
Table of Contents
Delphi now has a Ternary Operator using a new form of the If keyword
A or conditional operator is an operator which behaves like an if statement, with a condition and two possible values. In many other programming languages, the ternary operator is indicated using the ?: syntax. In Delphi, we want to preserve as much as possible a familiar and Pascal-oriented syntax, so we decided to use the if symbol as an operator. In other words, if can now be used to indicate a statement or an operator, depending on the position in the source code.
This is an example of a simple assignment expression in two versions, the first based on a traditional if statement and the second on an if operator:
// classic if statement if Left < 100 then X := 22 else X := 45; // assignment with if operator X := if Left < 100 then 22 else 45;
// classic if statement if Left < 100 then X := 22 X := 45; // assignment with if operator X := if Left < 100 then 22 else 45; |
The key difference is that the if operator can be used as part of any expression, as follows:
ShowMessage (if Left < 100 then 'Small' else 'Big')
ShowMessage (if Left < 100 then 'Small' else 'Big') |
Here you can see this code in the editor of the 64-bit version of the RAD Studio IDE, using the RAD Studio 13 beta.

The if operator in action in the RAD Studio 13 editor (in the 64-bit version of the IDE)
What are the compatibility rules of the Delphi Ternary Operator?
There are specific rules in terms of the coherence among the types of the two sub-expression. In short, they have to be of the same type, or a directly assignable type. If not you’ll get a compiler error, like in this case:
[dcc32 Error] Unit31.pas(38): E2010 Incompatible types: 'string' and 'Integer'

An example of an if operator incompatibility error
There is an extremely long table in the beta documentation with the different type combinations you can use in the new if expression, and the resulting type of the expression for each of these combinations. I’m not reporting it here as it’s a couple of pages long and goes to a level of detail not needed for this introduction.
What is the Order of Priority for the Delphi Ternary Operator?
When using the if-then-else operator within an expression, notice how other operations (e.g., addition) have higher priorities, which can lead to unexpected errors. Use extra parentheses, like you often have to do with low-priority Boolean operators. Let’s look at this code:
ShowMessage (if Left < 100 then 'Small' else 'Large' + 'Nice')
ShowMessage (if Left < 100 then 'Small' else 'Large' + 'Nice') |
What’s going to be the output if Left is less than 100? It is “Small” or “SmallNice“? It’s the former, because the + has higher priority making it like:
ShowMessage (if Left < 100 then 'Small' else ('Large' + 'Nice'))
ShowMessage (if Left < 100 then 'Small' else ('Large' + 'Nice')) |
if you always want the additional word to be added, you should use:
ShowMessage ((if Left < 100 then 'Small' else 'Large') + 'Nice')
ShowMessage ((if Left < 100 then 'Small' else 'Large') + 'Nice') |
While this won’t be the only change to the Delphi language in RAD Studio 13, it is one of the most prominent and requested changes to the language we are going to introduce. There are other new operators coming along, and some new and expanded compiler directives. Overall Delphi 13 (part of RAD Studio 13) is going to be a very important release for the evolution of the Delphi Object Pascal language. Stay tuned.
This blog post is based on a pre-release version of the RAD Studio software. No feature is committed until the product’s GA release.

Why not and see why we think it’s the fastest, easiest, and most efficient way to create cross platform apps that work on Windows, macOS, Linux, iOS and Android from a single code base?
Источник: