后续问题:Declaring a type as a subset of a set
program Project31; {$APPTYPE CONSOLE}
type
TDay = (mon, tue, wen, thu, fri, sat, sun);
TWeekday = mon..fri;
TDays = set of TDay;
TWeekdays = set of TWeekday;
TDayRec = record
Data: TDays;
WeekendData: string;
class operator Add(const A: TDayRec; B: TWeekday): TDayRec;
end;
class operator TDayRec.Add(const A: TDayRec; B: TWeekday): TDayRec;
begin
Result:= A;
Result.Data:= Result.Data + B; <<--E2008 Incompatible types
Result.Data:= Result.Data + TDay(B); <<--E2008 Incompatible types
end;
begin
end.
如何让TWeekday(s)
与TDay(s)
兼容。
将TWeekday
投射到TDay
不起作用。
答案 0 :(得分:4)
您有两种选择。您可以使用set union operator +
,它要求两个操作数都是兼容集。
Result.Data:= Result.Data + [B];
Include(Result.Data, B);
因为Include
运算符向集合中添加了单个元素,所以它能够生成比union运算符更高效的代码。