Search code examples
delphienumsdelphi-xe6

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. 🕗


Solution

  • 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;