Search code examples
delphidelphi-7

set of WideChar: Sets may have at most 256 elements


I have this line:

const
  MY_SET: set of WideChar = [WideChar('A')..WideChar('Z')];

The above does not compile, with error:

[Error] Sets may have at most 256 elements

But this line does compile ok:

var WS: WideString;
if WS[1] in [WideChar('A')..WideChar('Z')] then...

And this also compiles ok:

const
  MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')];
  ...
  if WS[1] in MY_SET then...

Why is that?

EDIT: My question is why if WS[1] in [WideChar('A')..WideChar('Z')] compiles? and why MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')]; compiles? aren't they also need to apply to the set rules?


Solution

  • A valid set has to obey two rules:

    1. Each element in a set must have an ordinal value less than 256.
    2. The set must not have more than 256 elements.
    MY_SET: set of WideChar = [WideChar('A')..WideChar('Z')];
    

    Here you declare a set type (Set of WideChar) which has more than 256 elements -> Compiler error.

    if WS[1] in [WideChar('A')..WideChar('Z')]
    

    Here, the compiler sees WideChar('A') as an ordinal value. This value and all other values in the set are below 256. This is ok with rule 1.

    The number of unique elements are also within limits (Ord('Z')-Ord('A')+1), so the 2nd rules passes.

    MY_SET = [WideChar('A')..WideChar('Z'), WideChar('a')..WideChar('z')];
    

    Here you declare a set that also fulfills the requirements as above. Note that the compiler sees this as a set of ordinal values, not as a set of WideChar.