Monday, May 07, 2007

DN4DP#8: Unicode identifiers

This post continues the series of The Delphi Language Chapter teasers from Jon Shemitz’ .NET 2.0 for Delphi Programmers book. Last time we included the section on the new inlining support of the compiler. This post briefly covers the internationalization of Pascal identifiers (Klingon code, anyone?).

Note that I do not get any royalties from the book and I highly recommend that you get your own copy – for instance at Amazon.

"Unicode identifiers

Traditional Pascal and Delphi has restricted identifier names to be lower and upper case ASCII letters (a-z), underscore and digits. In .NET all strings are Unicode and identifiers can use Unicode characters, including national characters, such as the Norwegian Æ, Ø and Å.

Delphi now supports Unicode characters in identifiers, as long as the source file is encoded in a Unicode format (UTF-8 or UTC2). Identifiers that end up in RTTI data (unit names, class names and published members) must still be pure ASCII - the main reason is to avoid breaking code that read RTTI strings.

TUnicodeClass = class
FAntallÅr: integer;
procedure SetAntallÅr(const Value: integer);
property AntallÅr: integer read FAntallÅr write SetAntallÅr;

procedure TestÆØÅ;
Unicode: TUnicodeClass;
Unicode := UnicodeClass.Create;
Unicode.AntallÅr := 42;

Tip To change the encoding of a source file to UTF-8, right-click in the Delphi editor and select File Format | UTF-8, then save it.


No comments:

Copyright © 2004-2007 by Hallvard Vassbotn