Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use enhanced record types in Delphi instead of classes?

Delphi 2006 introduced new capabilities for records, making them more 'object-oriented'.

In which situations is the record type more appropriate for a design than a class type? Which advantage does it have to use these record types?

like image 518
mjn Avatar asked May 09 '09 14:05

mjn


People also ask

What is the difference between a class and a record?

The main difference between class and record type in C# is that a record has the main purpose of storing data, while class define responsibility. Records are immutable, while classes are not.

What is the difference between class and record in Java?

Like enum , record is also a special class type in Java. It is intended to be used in places where a class is created only to act as plain data carrier. The important difference between class and record is that a record aims to eliminate all the boilerplate code needed to set and get the data from instance.

What is record in Delphi?

Records are a useful and distinguishing feature of delphi. They provide a very neat way of having named data structures - groups of data fields. Unlike arrays, a record may contain different types of data. Records are fixed in size - the definition of a record must contain fixed length fields.

What is the use of record in C#?

Beginning with C# 9, you use the record keyword to define a reference type that provides built-in functionality for encapsulating data. C# 10 allows the record class syntax as a synonym to clarify a reference type, and record struct to define a value type with similar functionality.


2 Answers

You have records, objects and classes.

Records are available since turbo pascal 1. They are lightweight, capable of having properties and methods, but they do not support inheritance, There are some issues with functions that return records. If these records have methods this sometimes gives internal errors:

type
  TRec = record 
    function Method1: Integer;
  end;

function Func: TRec;


procedure Test;
var
  x : TRec;

begin
  Func.Method1; // Sometimes crashes the compiler
  // Circumvention:
  x := Func;
  x.Method1; // Works
end;

Objects are introduced with turbo pascal 5 if I'm correct. They then provided a way for OO with pascal. They are more or less deprecated with the introduction of Delphi, but you can still use them. Objects can implement interfaces.

Classes are introduced with Delphi 1 and the most versatile. They implement interfaces and support inheritance. But each class variable is a hidden pointer. This means that classes need to be created on the heap. Luckily this process is mostly hidden.

Below is a table with the differences between the three. I added the interface for completion.

                  |Class|Object|Record|Interface|
------------------|-----------------------------|
Are pointers?     |  y  |  n   |  n   |    y    |
Inheritance       |  y  |  y   |  n   |    y    |
Helpers           |  y  |  n   |  y   |    n    |
Impl. Interface   |  y  |  y   |  n   |    -    |
Visibility        |  y  |  y   |  n   |    n    |
Method            |  y  |  y   |  y   |    y    |
Fields            |  y  |  y   |  y   |    n    | 
Properties        |  y  |  y   |  y   |    y    |
Consts            |  y  |  y   |  y   |    n    |
Types             |  y  |  y   |  y   |    n    |
Variants          |  n  |  n   |  y   |    n    |
Virtual           |  y  |  n   |  y   |    -    |
------------------|-----------------------------|
like image 153
Toon Krijthe Avatar answered Sep 19 '22 03:09

Toon Krijthe


I think those features were also available in Delphi 8 and 2005.

Main guideline: if you're in doubt, use a class.

For the rest you have to understand the main difference: Class Objects are always used through a reference, and are created by calling a Constructor.

The memory management and allocation for Records is the same as for the basic types (ie integer, double). That means that they are passed to methods by value (unless var is used). Also you don't need to Free records, and that's the reason they support operator overloading. But no inheritance or virtual methods etc. The new Records can have a constructor but it's use is kind of optional.

The main areas and criteria for using records:

  • when dealing with structs from the Win32 API

  • when the types don't have identity (because assignment means copying)

  • when the instances aren't too large (copying big records becomes expensive)

  • when building value types, whose behaviour should mimic the numerical types. Examples are DateTime, Complex Numbers, Vectors etc. And then operator overloading is a nice feature, but don't make that the deciding factor.

And efficiency-wise, don't overdo this:

  • for smaller types that you put in arrays often.

And finally, the rules for using a class or a records haven't really changed form the earlier versions of Delphi.

like image 39
Henk Holterman Avatar answered Sep 22 '22 03:09

Henk Holterman