Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a type as a subset of a set

Tags:

set

delphi

I can easily declare a enumeration and a set.
But sometimes I want to work with only part of the enumeration and I'd like the compiler to check for me if values in the sub-enum and its subset stay within the bounds.

type
  TDay = (mon, tue, wen, thu, fri, sat, sun);
  TWeekday = (mon..fri); //not allowed;

  TDays = set of TDay;
  TWeekdays = set of TDay[mon..fri]; //not allowed

Can I declare TWeekday and TWeekdays as a derivative of TDay, if so, how?

Funny enough google does not yield anything (for me) on this issue, just plain old sets.

like image 913
Johan Avatar asked Dec 19 '22 04:12

Johan


1 Answers

You've got the wrong syntax for the subrange. Drop the brackets () and it will work.

type
  TDay = (mon, tue, wen, thu, fri, sat, sun);
  TWeekday = mon..fri; // A subrange of TDay

  TDays = set of TDay;
  TWeekdays = set of TWeekDay; 

More about Subrange Types and Sets.

like image 69
LU RD Avatar answered Dec 24 '22 01:12

LU RD