ardnew
ardnew

Reputation: 2086

Create a set containing every member

Is there a way to create at compile-time (or with constant time when run) a set with members of ordinal type T containing every named value of T?

In other words, how could I complement the empty set of a particular type?

type 
  TEnum = 
  ( 
    eA = 1,
    eB = 5,
    eC = 34 
  );
  TSet = set of TEnum;

const
  CSet: TSet = ~[]; // with ~ being my fictional set complement operator

Then CSet should contain only the named values eA, eB, and eC.

Of course this isn't a practical question, I'm just curious


EDIT

I didn't realize the behavior of enum types when declared with explicit, non-consecutive values. The enum still contains unnamed members to fill the gaps. Updated question to apply only to named members

Upvotes: 4

Views: 226

Answers (1)

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28541

This is quite easy for enumerations that don't have specified values like

type 
  TEnum = 
  ( 
    eA,
    eB,
    eC 
  );
  TSet = set of TEnum;

const
  CSet: TSet = [eA..eC];
  CSet: TSet = [low(TEnum)..high(TEnum)];

However, with your TEnum defined as

type 
  TEnum = 
  ( 
    eA = 1,
    eB = 5,
    eC = 34 
  );

above will not work the way you expect. In your case CSet will contain all numerical values between low and high enum values (1 to 34).

The only way to get only TEnum values you have explicitly named is by using CSet: TSet = [eA, eB, eC];

This is by design as documented in Simple Types

Upvotes: 10

Related Questions