Originally Posted by
Eric
At the risk of starting another flame war, I would say some form of templates could be very useful. C++-like templates.
...
It also makes a simple generic sorter a creation of hideous complexity, as you'll need a generic IComparer<T> (cf. the RTL source...), while writing a template-based sorter can be trivial. Same goes for most algorithms. Soon enough, you find yourself piling up generic interfaces.
Now, all isn't rosy with templates, as templates can use other templates, so it's possible to get at the specialization errors that relate to a sub-sub-sub-template of the template you're using, with an error message that can be eminently cryptic because it bears only very indirect relation to what you were trying to do.
I wrote here about an include-based technique which can be used to create generic containers and algorithms.
For algorithms it works fine and is simple to use:
Code:
procedure Sort(Count: Integer; Data: array of TSortData);
type
_SortDataType = TSortData;
const
// Algorithm compile-time options
_SortOptions = [soDescending];
// Optional comparator function needed if TSortData cannot be compared directly with "<" operator
function _SortCompare(const V1,V2: TSortData): Integer; inline;
begin
Result := V1.Value - V2.Value;
end;
{$I gen_algo_sort.inc} // include type-independent sort implementation
end;
I forgot to mention another useful feature of recent Delphi versions - full-blown RTTI.
It can be used for many things, including dynamic code update, when you can compile new version of, for example, character AI class and dynamically update it in application/editor to immediately test the new behaviour without even restarting a level.
Bookmarks