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