views:

988

answers:

4

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?

+1  A: 

You can use operator overloading (like implicit conversions). This you can do on Delphi 2007+ or 2006.NET on objects too, but only on these records on 2006 win32.

DiGi
+6  A: 

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.

Henk Holterman
They are available for Delphi.NET platform, but not for win32 Delphi until 2007.
DiGi
mjustin wasn't specific about what features, but properties, methods and public/private were around for D2005/win32
Henk Holterman
+6  A: 

In addition to the other answers (operator overloading, lightweight value types), it's a good idea to make your enumerators records instead of classes. Since they're allocated on the stack, there's no need to construct and destruct them, which also removes the need for the hidden try..finally block that the compiler places around class-type enumerators.

See http://hallvards.blogspot.com/2007/10/more-fun-with-enumerators.html for more information.

Mason Wheeler
+10  A: 

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   |    -    |
------------------|-----------------------------|
Gamecat
Could you say if objects are still supported in Delphi? I know of "procedure xx of object;" but can't remember seeing them anywhere else.
Henk Holterman
Yes objects are still supported in D2009. There are no object helpers, but objects can implement interfaces.
Gamecat
And "procedure xx of Object" contruct is for declaring events signatures for methods, AFAIK doesn't have nothing to do with objects.
Fabricio Araujo
Cool, never knew that objects can implement interfaces. Have to look quickly if FPC does.
Marco van de Voort