Helpers extend a type vertically

Helpers extend a type vertically

On page 67 in 'Expert Delphi', Pawel says "Manipulating types in code can be even more readable when we define type helpers. Helpers extend a type vertically."

What does he mean by "vertically"?

I've included below the code snippet used in the book as a reference. I just can't wrap my head around the meaning of extending something vertically.

{begin code snippet}
var s: string; i: integer;
begin
i := 10;
s := IntToStr(i); // "old" style
s := i.ToString; // more readable
s := 10.ToString; // that would work too
{end code snippet }

Comments

  1. I think he means you can add new methods to the class without having to create a new class inheriting from the original one.

    ReplyDelete
  2. Pretty hard to see how i.ToString is "more" readable that IntToStr(i) frankly. At best that is the subjective opinion aligned with the designers of C#/Java. The Python designers took a very different stance.

    ReplyDelete
  3. i.ToString is not inherently more readable than IntToStr(i), but it is more discoverable. At least at times when code completion works.

    ReplyDelete
  4. Steve Sinclair Of course, the limitation to a single helper means that in practice you can't extend these fundamental types. Or indeed any type that already has a helper that you want to use.

    ReplyDelete
  5. David Heffernan Well, class helpers actually can be inherited. So you can easily extend for example a TDataSetHelper = class helper for TDataSet, by writing TMyDataSetHelper = class helper (TDataSetHelper) for TDataSet. Unfortunately that doesn't work for record helpers or those for intrinsic types like Integer.

    ReplyDelete
  6. Uwe Raabe and you have to be extremely careful with scope too.

    ReplyDelete
  7. It is more readable in terms of usually you read better into one direction. So when you have code like IntToStr(customer.Orders.Count) you actually have to backtrack and change context. When you read (and write) customer.Orders.Count.ToString it simply goes from left to right.

    ReplyDelete

Post a Comment