So writing an example for a newbie on IRC, I was reminded how TList doesn't have a Sort overload that takes a TComparison function reference directly.

So writing an example for a newbie on IRC, I was reminded how TList doesn't have a Sort overload that takes a TComparison function reference directly.


At the very least they could have provided a version which just made the TDelegatedComparer for you... the current way leads to such fugly code.

Comments

  1. Just use Spring4D: generic containers done well.

    ReplyDelete
  2. Jeroen Wiert Pluimers Just because some 3rd party library is great doesn't mean the built-in stuff has to suck, does it?

    ReplyDelete
  3. Asbjørn Heid  Of course not, but in some cases, it does anyway.

    Not that Spring4D is much better; there's a fundamental limitation at the language level that prevents this particular problem from being handled correctly.

    Put simply, these "comparers" should not exist at all.  The Generics system should understand standard operators such as =, <>, <, >, etc, and should have "operator constraints" defined to ensure that types that are going to be compared already have these operators defined, either at the language level or via operator overloads, in order to be used.  I've been requesting this since D2009, but it's been ignored, to the detriment of everyone who works with Generics on any but the most trivial levels.

    This is one of the problems of dotnetification: blindly copying the bad parts along with the good.  .NET generics don't have this, and are stuck with ugly crap like comparers... and therefore Delphi's should as well?  What was the team thinking?

    ReplyDelete
  4. Mason Wheeler That would require classes to have these operators aswell. Not a bad thing but since we are used to comparers in the form of passing a compare function to sort methods I don't have that big of a problem with that.
    However the default comparers should consider operator overloads on records and TObject should not only have an Equals but also a Compare method to make it unnecessary to explicitly define the comparison when sorting.
    Something we partly solved in Spring4D by offering a comparer that checks for IComparable being implemented. And I have been working on a comparer for records that looks for operator overloads first.

    ReplyDelete
  5. Mason Wheeler Clearly concepts ala C++ or similar would be preferable, but lets start with the fruit touching the grass.

    ReplyDelete

Post a Comment