4

Is there a trick to pass records with different type as parameter in a procedure? For example, look at this pseudo-code:

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;
1
  • Records do not support inheritance, please read the fine manual. Commented Feb 27, 2015 at 12:30

1 Answer 1

8

Record instances don't contain type information in the same way that classes do. So you would need to pass an extra argument to indicate which type you were working with. For instance:

type
  TRecordType = (rtPerson, rtSpider);

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

You might contemplate putting the type code in the first field of each record:

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;

You could use generics:

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;

And call the functions like this:

TMyRecordDispatcher.DoSomething(APerson);
TMyRecordDispatcher.DoSomething(ASpider);

This uses generic type inference and so allows you not to explicitly state the type. Although as an example of generics it makes me cringe. Please don't do this.

In my view all of this is messy and brittle. Much of the above reimplements run time method dispatch, polymorphism. Classes are more suited to this. I don't endorse any of the code above.

On the other hand, perhaps this is all needless. What's wrong with:

DoSomethingWithThisPerson(Person);
DoSomethingWithThisSpider(Spider);

Since you know the types at compile time, why opt for anything more complex?

You could use function overloading to make it possible to omit the type from the function name.

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

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

....

DoSomething(Person);
DoSomething(Spider);
13
  • Wow, that was fast and ingenious/brilliant! Thanks! Commented Feb 26, 2015 at 19:21
  • Declaring classes produces a lot of code. Then objects need to be created from them and then properly freed. All that takes a lot of work and coding. RECORDS are so easy and simple to work with, no hassle, not much code. So in many cases I prefer records. Commented Feb 26, 2015 at 19:28
  • 1
    In this scenario the records are going to lead to more code and less type safety than the class based alternative. The record vs class distinction largely comes down to what you want the assignment operator to mean. Do you want it to mean copy or take a reference? That's really the thing with records and classes in my view. The other thing that tends to drive the decision is what you are facing. Records don't support polymorphism. Classes do. You want polymorphism. Either get it for free with classes or implement it yourself without the safety net of the compiler. Commented Feb 26, 2015 at 19:29
  • 3
    I agree with David Heffernan about using classes instead in your case becouse your shown record design does indicate that you might be better of by using polymorphism. But if you realy want to use records instead you could go and make use of method overloading. This means that you will have multiple methods with same name but different parameters. All of them must be folowed by Overload command. This way your program will automatically decide which method to use based on paramteres provided to it. Commented Feb 26, 2015 at 19:52
  • 3
    I think that variants are the spawn of satan Commented Feb 26, 2015 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.