Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class methods in Record vs Class in Delphi 2010

I just started to play with the new unit in Delphi 2010 IOUtils.pas, and I found they put all the methods inside Records(TFile, TPath, TDirectory) as class functions and procedure.

Is there any benefits of doing that inside records instead of classes? In both cases there's no need for any variables or instance, but I'm not sure if there any real benefits regarding memory consuming or performance improvement.

like image 255
Mohammed Nasman Avatar asked Mar 07 '11 13:03

Mohammed Nasman


1 Answers

Class methods in records are used to group different methods into a common namespace. Thus you can have similar named methods for different purposes. For an example in IOUtils.pas look at the Exists function available in TFile and in TDirectory. The older approach was to have distinct function names for FileExists and DirectoryExists (which the implementations actually call).

While class methods inside classes can be used in the same way, they can in addition have another goal: they can be virtual. Called from a class variable this can lead to different implementations depending on the current content of that variable. This is not possible for records. As a consequence class methods in records are always static.

like image 103
Uwe Raabe Avatar answered Oct 20 '22 11:10

Uwe Raabe