Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the destructor in Delphi named?

Destructors in Delphi are usually named "Destroy", however as far as i understand you can also

  • name destructors differently
  • have multiple destructors

Is there any reason why this was implemented this way? What are the possible use cases for differently named / multiple destructors?

like image 922
Askaga Avatar asked Nov 12 '13 10:11

Askaga


Video Answer


1 Answers

In theory you can manually call different destructors to free different external resources, like breaking ref-counting loops, deleting or just closing file, etc.

Also, since the Object Pascal language does not have those magical new/delete operations, there just should be some identifier to call for disposing of the object.

I'd prefer to look at that in retrospect.

"Turbo Pascal with Objects" style objects have both - you call a "magical" Dispose procedure but explicitly specify a destructor to call, since language itself did not knew what to choose. Similarly "magic" procedure New had to be supplied with a manually selected constructor.

  • http://www.freepascal.org/docs-html/rtl/system/dispose.html
  • http://putka.acm.si/langref/turboPascal/0547.html
  • http://www.freepascal.org/docs-html/rtl/system/new.html
  • http://putka.acm.si/langref/turboPascal/04A4.html

This however violates DRY principle: compiler knows that we are calling d-tor or c-tor, but yet we have to additionally call those "New" and "Dispose" functions. In theory that probably provided to decouple memory allocation and information feeding and combine them anyway we'd like. But i don't think this feature was actually used anything wide.

Interesting that the same design is used in Apple Objective C. You 1st allocate memory for the object and after that you call a constructor for that new instance: http://en.wikipedia.org/wiki/Objective-C#Instantiation

When that model was streamlined for Delphi few decisions was made to make things more simplified (and unified). Memory [de]allocation strategy was shifted to the class level, rather than call-site. That made the redundancy of both calling "New" and named constructor very contrast. One had to be dropped.

C++/C#/Java chosen to retain a special language-level keywords for it, using overloaded functions to provide different c-tors. Perhaps that corresponds to USA style of computer languages.

However Pascal at its core has two ideas: verbosity and small vocabulary. Arguably they can be tracked in other European-school languages like Scala. If possible, the keywords should be removed from language itself and moved to external modules - libraries that you can add or remove from project. And overloaded functions were introduced much later to the language and early preference was to surely have two differently named (self-documenting) function names.

This both ideas probably caused Delphi to remove "magic" procedures and to deduce object creation/destruction at the call-site just by used function names. If you call MyVar.Destroy then compiler looks at the declaration of .Destroy and knows we are deleting the object. Similarly it knows TMyType.CreateXXX(YYY,ZZZ) is an object instanbtiation due to the way CreateXXX was declared.

To make c-tor and d-tor no-named like in C++, Delphi would have to introduce two more keywords to the language level, like those C++ new and delete. And there seems to be no clear advantage in that. At least personally i better like Delphi way.

PS. I had to add there one assumption: we are talking about real C++ and Delphi languages as they were around 1995. They only featured manual memory control for heap-allocated objects, no garbage collection and no automatic ref-counting. You could not trigger object destruction by assigning variable with nil/NULL pointer.

like image 185
Arioch 'The Avatar answered Oct 17 '22 22:10

Arioch 'The