Jump to content
Sign in to follow this  
Larry Hengen

Delphi Parser/Compiler Limitation?

Recommended Posts

Posted (edited)

I am implementing a REST API Client for fun and it uses ISO Country  and Currency Codes.  I wanted to use enums, and GetEnumName() for display rather than prefixed enum values and a constant array of strings to get their string equivalents.  I like Scoped enums as it's easier to find the enum value applicable without getting suggestions of other enums that use the same prefix.  This may no longer be an issue with the LSP, but I still prefer it.  Unfortunately, Delphi flags any enum values that are also keywords as you can see in the code below.  Is the only way to work around this an enum and const array of string?

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

{$SCOPEDENUMS ON}   //many enums use same value name so scope them to eliminate conflicts
type
  TEnums = (TRY, SHR, XOR, IS, AS);         //results in compiler error "[dcc32 Error] Project2.dpr(12): E2029 Identifier expected but 'TRY' found"
  //same for all other values as they are keywords.  Why can't the parser deal with this?  It's a real problem with ISO country and currency codes
  //would be nice to use GetEnumName() instead of creating a constant array [TCountryCode] of strings....much DRYer

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

 

Edited by Sherlock
Inserted code tags...please use them next time

Share this post


Link to post
Posted (edited)

Use ampersand operator, Luke.

 program Project2;
{$APPTYPE CONSOLE}
uses
  System.TypInfo;
{$SCOPEDENUMS ON}
type
  TEnums = (&TRY, &SHR, &XOR, &IS, &AS);
begin
  WriteLn(GetEnumName(TypeInfo(TEnums), Ord(TEnums.SHR)));
  WriteLn(GetEnumValue(TypeInfo(TEnums), 'shr'));
  ReadLn;
end.

Output:

SHR
1
Edited by Kazantsev Alexey
  • Like 2
  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×