Sets in Delphi XE are not working the same way they worked in D7
Asked Answered
I

2

7

I had this constants in a Delphi 7 program. They are not compiling under Delphi XE.

TYPE
  TSingleChar= AnsiChar;

CONST
  noData: TSingleChar= '.';
  Ambiguity= ['x'];
  DNA_Ambig= ['x', noData]+ Ambiguity;

[DCC Error] E2026 Constant expression expected.

  1. What was changed in XE that my old code does not compile?
  2. I suppose that the code, as it is, is interpreted as Unicode. Am I correct?
Iveson answered 8/7, 2011 at 12:42 Comment(10)
I assume you "sanitized" the code too much before posting it; What you posted compiles without error on Delphi 7, Delphi 2010 and Delphi XE.Tablet
I just tried it in Delphi 2009 (which is Unicode), and it compiles fine.Looseleaf
@Altar: Are you sure that Ambiguity really is declared as const? Are you sure that you do exactly like above in your actual code?Looseleaf
About your point (2), characters in sets are actually interpreted as AnsiChar, not Unicode; Try compiling this and look at the error message: Ambiguity = ['Б','В','Г','Ґ','Д','Ђ']Tablet
There is an interesting point here though. Why is Delphi incapable of recognising a typed constant as being constant? Is it a hangover from that horrid oxymoron, the writeable typed constant?Enculturation
@Altar: I'd suggest you open your test unit and press Ctrl-O-O to insert all compiler options at the top. So you can be sure everybody tests the same.Amitie
@Downvoters, probably time to revoke the downvotes. Maybe even upvote.Tablet
@Cosmin - Are you sure this is any different in D7? If not the question title can still be a reason for a downvote.Squalene
@Sertac, it works absolutely the same in Delphi 7, 2010 and XE. Altar should also edit the title!Tablet
-1. Reported code hasn't compiled in any version.Cyclosis
T
8

"Fix" it like this:

TYPE
  TSingleChar= AnsiChar;

CONST
  Const_noData = '.';
  noData: TSingleChar= Const_noData;
  Ambiguity= ['x'];
  DNA_Ambig= ['x', Const_noData]+ Ambiguity;

The Const_noData is a true const as far as the compiler's concerned, allowing you to initialize both noData and DNA_Ambig using it. And you still respect the DRY principle, ie, there's only one definition for noData, the Const_noData.

Tablet answered 8/7, 2011 at 13:6 Comment(3)
This is just so depressing to me. I long for some future nirvana when constants really are, you know, constant.Enculturation
Thanks. That solved the issue. (Well the code now looks pretty ugly since I have lots of constants and now I have two declarations for each of them... but I guess I have to accept this).Iveson
You could avoid having two declarations, Altar, but removing the typed constants.Cyclosis
L
7
const
  Ambiguity:  TAnsiCharSet = ['B', 'D', 'H'];
  Ambiguity2: TAnsiCharSet = ['C', 'c', 't'] + Ambiguity;

does not work.

const
  Ambiguity = ['B', 'D', 'H'];
  Ambiguity2 = ['C', 'c', 't'] + Ambiguity;

does work. Typed constants aren't really constants at all...

(Notice that the issue has nothing to do with ambiguity. It is about what is considered a constant and what is not.)

Looseleaf answered 8/7, 2011 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.