Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Delphi to union enumerations into a larger enumeration?

Delphi can have enumerated types, e.g.:

type
   TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);   // Enumeration values

Is it possible to union enumerated types:

type
   TWeekDay    = (Mon, Tue, Wed, Thu, Fri);
   TWeekendDay = (Sat, Sun);
   TDay        = (TWeekday, TWeekendDay);    //hypothetical syntax

In reality, i need to decompose a large list into the disjoint items they actually are, without breaking source-code compatibility:

type
   TWeekDay =    (Mon, Tue, Wed, Thu, Fri);
   TWeekendDay = (Sat, Sun);
   TDay =        (Mon, Tue, Wed, Thu, Fri, Sat, Sun); //identifier redeclared syntax error

And then change some variables:

  • Day: TWeekday;TDay;
  • Day: TWeekendDay;TDay

It's sort of the moral equivalent of strict typing. 🕗

like image 684
Ian Boyd Avatar asked Jan 02 '20 20:01

Ian Boyd


People also ask

What are the advantages of using enumerations?

The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

What are the advantages of enumerated data types?

Advantages of Enumerated Data Type:Makes program more readable to the fellow developers. Makes program more manageable while dealing enum type of variables. Using enumerators reduces programming error.


1 Answers

The answer is "No".

But a workaround available to you, if subrages are contiguous, is to use subranges:

TDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); 

TWeekDay2 = Mon..Fri; 
TWeekday = type TWeekDay2;

TWeekendDay2 = Sat..Sun;
TWeekendDay = type TWeekendDay;
like image 113
Ian Boyd Avatar answered Oct 18 '22 03:10

Ian Boyd