【发布时间】:2026-01-21 22:10:02
【问题描述】:
我正在使用记录来封装两个不同的集合。
我已经加入了运算符,以允许将任一集合分配给记录。这样做会清除另一组。
但是我不能分配一个空集。
参见以下示例代码:
Program test;
{$Apptype console}
type
TSomeThing = (a,b,c);
TOtherThing = (x,y,z);
TSomeThings = set of TSomething;
TOtherThings = set of TOtherThing;
TSomeRecord = record
strict private
Fa: TSomeThings;
Fb: TOtherThings;
public
class operator Implicit(a: TSomeThings): TSomeRecord;
class operator Implicit(a: TOtherThings): TSomeRecord;
end;
implementation
class operator TSomeRecord.Implicit(a: TSomeThings): TSomeRecord;
begin
Result.Fa:= a;
Result.Fb:= [];
end;
class operator TSomeRecord.Implicit(a: TOtherThings): TSomeRecord;
begin
Result.Fa:= [];
Result.Fb:= a;
end;
var
SomeRec: TSomeRecord;
begin
SomeRec:= [];
end.
[dcc64 错误] InstructionList.pas(512): E2010 不兼容的类型:'TSomeRecord' 和 'Set'
如何才能将空集分配给我的记录?
我可以滥用隐式运算符来允许SomeRec:= nil;,但这看起来很丑陋。
【问题讨论】:
-
编译器无法识别
[]是什么。是TSomeThings还是TOtherThings?甚至我们也不能说。只有你知道你要在那里分配什么。
标签: delphi set operator-overloading