子集与其超集不兼容

时间:2015-04-23 11:13:03

标签: delphi set

后续问题: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不起作用。

1 个答案:

答案 0 :(得分:4)

您有两种选择。您可以使用set union operator +,它要求两个操作数都是兼容集。

Result.Data:= Result.Data + [B];

或者您可以使用intrinsic程序Include

Include(Result.Data, B);

因为Include运算符向集合中添加了单个元素,所以它能够生成比union运算符更高效的代码。