Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a set containing every member

Tags:

delphi

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

like image 782
ardnew Avatar asked Dec 25 '22 12:12

ardnew


1 Answers

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

like image 131
Dalija Prasnikar Avatar answered Jan 11 '23 11:01

Dalija Prasnikar