将不同的记录类型作为参数传递给程序?

use*_*348 4 delphi record delphi-xe7

是否有一个技巧可以将不同类型的记录作为参数传递给过程?例如,看看这个伪代码:

type
  TPerson = record
    Species: string;
    CountLegs: Integer;
  end;

  TSpider = record
    Species: string;
    CountLegs: Integer;
    Color: TColor;
  end;

var
  APerson: TPerson;
  ASpider: TSpider;

// Is there a trick to pass different record types as parameter in a procedure?:
procedure DoSomethingWithARecord(const ARecord: TAbstractRecord?);
begin
  if ARecord is TPerson then
    DoSomethingWithThisPerson(ARecord as TPerson)
  else if ARecord is TSpider then
    DoSomethingWithThisSpider(ARecord as TSpider);
end;  

procedure DefineRecords;
begin
  APerson.Species := 'Human';
  APerson.CountLegs := 2;
  ASpider.Species := 'Insect';
  ASpider.CountLegs := 8;
  ASpider.Color := clBtnFace;
  DoSomethingWithARecord(APerson);
  DoSomethingWithARecord(ASpider);
end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 8

记录实例不包含与类相同的类型信息.因此,您需要传递一个额外的参数来指示您正在使用的类型.例如:

type
  TRecordType = (rtPerson, rtSpider);

procedure DoSomething(RecordType: TRecordType; const ARecord);
begin
  case RecordType of
  rtPerson:
    DoSomethingWithThisPerson(TPerson(ARecord));
  rtSpider:
    DoSomethingWithThisSpider(TSpider(ARecord));
  end;
end;
Run Code Online (Sandbox Code Playgroud)

您可以考虑将类型代码放在每条记录的第一个字段中:

type
  TPerson = record
    RecordType: TRecordType;
    Species: string;
    CountLegs: Integer;
  end;

  TSpider = record
    RecordType: TRecordType;
    Species: string;
    CountLegs: Integer;
    Color: TColor;
  end;

function GetRecordType(ARecord): TRecordType;
begin
  Result := TRecordType(ARecord);
end;

....

procedure DoSomething(const ARecord);
begin
  case GetRecordType(ARecord) of
  rtPerson:
    DoSomethingWithThisPerson(TPerson(ARecord));
  rtSpider:
    DoSomethingWithThisSpider(TSpider(ARecord));
  end;
end;
Run Code Online (Sandbox Code Playgroud)

你可以使用泛型:

type
  TMyRecordDispatcher = record
    class procedure DoSomething<T: record>(const Value: T); static;
  end;

class procedure TMyRecordDispatcher.DoSomething<T>(const Value: T); 
begin
  if TypeInfo(T) = TypeInfo(TPerson) then
    DoSomethingWithThisPerson(PPerson(@Value)^)
  else if TypeInfo(T) = TypeInfo(TSpider) then
    DoSomethingWithThisSpider(PSpider(@Value)^);
end;
Run Code Online (Sandbox Code Playgroud)

并调用这样的函数:

TMyRecordDispatcher.DoSomething(APerson);
TMyRecordDispatcher.DoSomething(ASpider);
Run Code Online (Sandbox Code Playgroud)

这使用泛型类型推断,因此您不能显式声明类型.虽然作为泛型的一个例子,它让我感到畏缩.请不要这样做.

在我看来,所有这一切都是凌乱和脆弱的.以上大部分重新实现运行时方法调度,多态.类更适合这个.我不赞同上面的任何代码.

另一方面,也许这一切都是不必要的.有什么不对:

DoSomethingWithThisPerson(Person);
DoSomethingWithThisSpider(Spider);
Run Code Online (Sandbox Code Playgroud)

既然您在编译时知道类型,为什么选择更复杂的东西呢?

您可以使用函数重载来省略函数名称中的类型.

procedure DoSomething(const APerson: TPerson); overload;
begin
  ....
end;

procedure DoSomething(const ASpider: TSpider); overload;
begin
  ....
end;

....

DoSomething(Person);
DoSomething(Spider);
Run Code Online (Sandbox Code Playgroud)

  • 我同意David Heffernan关于在您的情况下使用类,因为您显示的记录设计确实表明您可能通过使用多态性更好.但是,如果您真的想要使用记录,那么您可以继续使用方法重载.这意味着您将拥有多个具有相同名称但参数不同的方法.所有这些都必须包含Overload命令.这样,您的程序将根据提供给它的参数自动决定使用哪种方法. (3认同)
  • 我认为变种是撒旦的产物 (3认同)