Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "&" operator mean in Delphi? [duplicate]

Tags:

delphi

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.

like image 213
Mehmet Fide Avatar asked Aug 07 '18 12:08

Mehmet Fide


2 Answers

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.)

like image 145
Andreas Rejbrand Avatar answered Oct 13 '22 11:10

Andreas Rejbrand


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.

like image 39
David Heffernan Avatar answered Oct 13 '22 10:10

David Heffernan