子集与其超集不兼容
Subset is incompatible with its superset
后续问题:
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
无效。
你有两个选择。您可以使用 set union operator +
,它要求两个操作数都是兼容的集合。
Result.Data:= Result.Data + [B];
或者您可以使用 intrinsic procedure Include
。
Include(Result.Data, B);
因为 Include
运算符将单个元素添加到集合中,所以它能够生成比联合运算符更高效的代码。
后续问题:
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
无效。
你有两个选择。您可以使用 set union operator +
,它要求两个操作数都是兼容的集合。
Result.Data:= Result.Data + [B];
或者您可以使用 intrinsic procedure Include
。
Include(Result.Data, B);
因为 Include
运算符将单个元素添加到集合中,所以它能够生成比联合运算符更高效的代码。