Using a reserved word as an identifier (variable name, class name etc) in Delphi by Scott Hollows

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Using a reserved word as an identifier (variable name, class name etc) in Delphi
Scott Hollows - 14/Oct/2016
[SHOWTOGROUPS=4,20]
AKA Silly Delphi Party Tricks #1

Is it possible to use a reserved word as an identifier ?
For example – I want to name my variable or class “BEGIN” because, ummm, well I dont know just because.

Yes you can. Simply prefix the the name with an ampersand “&” escape character like this
Код:
var
   &begin  : integer;  // begin is a reserved word
begin
   &begin  := 123;
end;

That doesn’t mean its a good idea. This is mainly useful to impress your friends at parties with your astounding knowledge of Delphi quirks.

Delphi doesnt always play nicely with this language feature. I dont mean it will crash – I just mean sometimes it will display the ampersand and sometimes it wont. For example if you add a breakpoint then view the variable in the debugger it will be show as &BEGIN.

escape_reserved_word_delphi_debugger


However, if you show the name of a class based on a reserved word at runtime it removes the ampersand
Код:
//     -- the name of this class is a reserved word "begin"
type
   &begin = class(TForm)
      private
        foo : integer;
      end;
begin
  ShowMessage ('The class name is '
              + &begin.ClassName
              + #10
              + 'length of class name = '
              + &begin.ClassName.Length.ToString
              );
end;

When the code is run, the class name “begin” is displayed (no ampersand).
The length is 5, which verifies the name BEGIN is being used and the ampersand escape character is not stored in the name.

escape_reserved_word_delphi_class_name


Is this used in Delphi’s own source code ?
You would think that this technique would be avoided in the Delphi source code but actually it is used in at least these 36 units (in Delphi 10.1) and probably more.

Delphi UnitReserved Word
used as a procedure,
function or property
Name
Box2D.CollisionSET
Box2D.CommonSET
FMX.AddressBook.AndroidTYPE
FMX.Clipboard.iOSSTRING
FMX.Edit.iOSEND
FMX.LayoutsEND
FMX.Media.AndroidSET
FMX.Media.AVFoundationRECORD
FMX.Platform.iOSEND
FMX.Platform.MacTYPE and SET
FMX.PushNotification.AndroidGET
IdFTPListParseStercomOS390ExpARRAY and SET
IdGlobalON
System.Android.BluetoothSET
System.Android.NotificationARRAY, OBJECT, TYPE
System.JSON.BSONCONSTRUCTOR
System.JSON.WritersON
System.Mac.BluetoothSET
System.Net.SocketFOR
System.ThreadingON, STRING, TYPE
System.Win.BluetoothTYPE
Vcl.ComCtrlsTYPE

Good enough for Delphi, good enough for me ?
By now you are thinking “hey, if its ok for this to be used in the source code of Delphi itself, surely it is ok for me to use this technique”.

Nahhhhh, dont be silly. The technique works, but that doesn’t mean it’s a good idea to use it. I think you should only use this in desperation. For example, writing code that has to closely match some other code or interface that is outside of your control.

Even in those scenarios, I prefer to prefix or suffix an underscore to the name to make it clear that I am avoiding the use of a reserved word. Like this :
Код:
type
   HokeyPockey = class (TForm)
      private
      public
        _in   : boolean;
        _out  : boolean;
        InOut : boolean;
        ShakeItAllAbout : boolean;
   end;
[/SHOWTOGROUPS]