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

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 Unit Reserved Word
used as a procedure,
function or property
Name
Box2D.Collision SET
Box2D.Common SET
FMX.AddressBook.Android TYPE
FMX.Clipboard.iOS STRING
FMX.Edit.iOS END
FMX.Layouts END
FMX.Media.Android SET
FMX.Media.AVFoundation RECORD
FMX.Platform.iOS END
FMX.Platform.Mac TYPE and SET
FMX.PushNotification.Android GET
IdFTPListParseStercomOS390Exp ARRAY and SET
IdGlobal ON
System.Android.Bluetooth SET
System.Android.Notification ARRAY, OBJECT, TYPE
System.JSON.BSON CONSTRUCTOR
System.JSON.Writers ON
System.Mac.Bluetooth SET
System.Net.Socket FOR
System.Threading ON, STRING, TYPE
System.Win.Bluetooth TYPE
Vcl.ComCtrls TYPE

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;

+1 this post

If you like this post, please +1 for me here on Delphi Feeds and here on BeginEnd

About The Author

scott_circle
The Usual Suspect
– Scott Hollows –

  • Oracle and Delphi software developer.
  • Australian Delphi User Group – Western Australia Chief Cat Herder
  • Australian Delphi User Group – President
blog email linkedinlogo

Author: Scott Hollows

Enterprise software developer based in Perth, Western Australia. Focused on Oracle, Delphi, Data Warehouse design and ETL, Data Architect, Business Intelligence, Oracle performance tuning. President of the Australian Delphi User Group (ADUG) LinkedIn www.linkedin.com/in/scotthollows

2 thoughts on “Using a reserved word as an identifier (variable name, class name etc) in Delphi”

  1. Good point. We dont know which test case to trust so Ive modified the code to also show the length of the class name. That shows 5 for &BEGIN so its looks like it is BEGIN not &BEGIN. The & is just an escape character that is not stored in the name

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s