Today I decided to use TParallel.For and successfully used in Delphi 10.2.3
But TParallel.For has a strange definition and this kind of definitions are new to me. Also I couldn't find meaning of it in Delphi documentations.
class function TParallel.&For(ALowInclusive, AHighInclusive: Integer; const AIteratorEvent: TProc<Integer>): TLoopResult;
What is the meaning of & Operator here? Is there any difference between using TParallel.&For or TParallel.For since both compiles.
The ampersand is used to "escape" reserved words. Consider the following example:
function GetPart: string;
const
LoremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +
'eiusmod tempor incididunt ut labore et dolore magna aliqua.';
var
start, end: integer;
begin
start := 2;
end := 10;
result := Copy(LoremIpsum, start, end - start + 1);
end;
Obviously, this won't compile, since end
is a reserved word and therefore cannot be used as an identifier.
However, if you still really want to use end
as the variable's name, you can prefix it with &
:
function GetPart: string;
const
LoremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +
'eiusmod tempor incididunt ut labore et dolore magna aliqua.';
var
start, &end: integer;
begin
start := 2;
&end := 10;
result := Copy(LoremIpsum, start, &end - start + 1);
end;
This tells the compiler, "hey, don't treat the next token as a reserved word".
Of course, one might argue that in most cases it is better to use a non-reserved word as an identifier instead of using the &
trick to escape the token. However, sometimes you need your identifiers to have names dictated by other frameworks and technologies, and so Delphi needs to support a way to allow using reserved words as identifiers.
(Personally, however, I do tend to use the &
trick in my own code when the identifier that I find would be the most natural choice happens to be a reserved word, even if no 'external constraints' require that name.)
This is described in the documentation:
Extended Identifiers
You might encounter identifiers (e.g. types, or methods in a class) having the same name as a Delphi language reserved word. For example, a class might have a method called begin. Delphi reserved words such as begin cannot be used for an identifier name.
If you fully qualify the identifier, then there is no problem. For example, if you want to use the Delphi reserved word type for an identifer name, you must use its fully qualified name:
var TMyType.type // Using a fully qualified name avoids ambiguity with {{Delphi}} language keyword.
As a shorter alternative, the ampersand (&) operator can be used to resolve ambiguities between identifiers and Delphi language reserved words. The & prevents a keyword from being parsed as a keyword (that is, a reserved word). If you encounter a method or type that is the same name as a Delphi keyword, you can omit the namespace specification if you prefix the identifier name with an ampersand. But when you are declaring an identifier that has the same name as a keyword, you must use the &:
type &Type = Integer; // Prefix with '&' is ok.
This text explains why the &
is not necessary when calling the method. You can write
TParallel.For(...);
and the compiler is able to resolve the ambiguity because you the identifier has been qualified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With