Guy Gordon
Guy Gordon

Reputation: 740

How can I overload simple Delphi Functions of Integers?

Using Delphi 7, I have several functions that take a single integer parameter. These functions operate on a Text string belonging to the object. The only parameter is either a character index in the Text, or a word index. For example, to retrieve the next word of Text after word i or after the character Text[loc] I want to have:

function NextWord(i:Integer): String; overload;
function NextWord(loc:Integer): String; overload;

Obviously, this is ambiguous. Delphi Help says "Overloaded routines must be distinguished by the number of parameters they take or the types of their parameters." So I defined two types:

type WordIndex = Integer;
type CharIndex = Integer;
...
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;

But this doesn't work, as these are just aliases for Integer.

Delphi Help on types distinguishes 'type identity', 'type compatibility', and 'assignment-compatibility', and then says nothing about how to use the distinction. But it does say to repeat the word 'type' to create new type not identical to Integer:

type WordIndex = type Integer;    //really, a new type
type CharIndex = type Integer;
...
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;
...
var WordNumber: WordIndex;      //variable of new type
...
  WordStr := NextWord(WordNumber);  //call overloaded function

To my surprise, this doesn't work either. The compiler recognizes WordIndex as a separate type, and compiles the overloaded functions, but where called it claims the overload is ambiguous. I also tried changing the function parameters to VAR (because "For var parameters, types of formal and actual must be identical."), but that didn't help.

Hitting the Web I read that the compiler disambiguates overloaded functions based first on the number of parameters, and second on their size. I suppose I could make WordIndex a DWord instead of Integer, but I don't want it to be 16 bits, and I'm using -1 to mean 'invalid word number'.

Finally, I found this in the Help for 'Overloading procedures and functions': "You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine's declarations, but that are assignment-compatible with the parameters in more than one declaration. ... In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters are of the type with the smallest range that accommodates the actual parameters in the call."

So I tried this, which works:

type WordIndex = -1..High(Integer);
type CharIndex =  Integer;
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;
...
var WordNumber: WordIndex;
    loc: CharIndex;
...
  WordStr := NextWord(WordNumber);
  (or)
  WordStr := NextWord(loc);

This compiles without warnings or errors, and does in fact call the correct function for each parameter type. Also SizeOf(WordIndex) = SizeOf(CharIndex) = 4. I.E. they're both Integers.

My question is, is this how I'm supposed to do it? And if so, how was I supposed to know that? Are there any examples like that in the Help or in the Source?

Bonus question: I checked, and making type WordIndex a Range creates no code/runtime overhead in the call or in the functions. But might it elsewhere in the code? Will Delphi be testing WordNumber for a Range Error anywhere it wouldn't be testing an Integer? (I'm pretty sure the answer is 'No'.)

Upvotes: 2

Views: 718

Answers (2)

alzaimar
alzaimar

Reputation: 4622

If you run into this type of problem, use refactoring to separate the different behavior. For example, you could split the word extraction by location and char index, you could introduce two new classes:

The call would be:

MyClass.Location.NextWord(SomeIndex);
MyClass.Character.NextWord(SomeIndex);

Another approach could be to provide the method using a second parameter:

MyClass.NextWord(SomeWordIndex, wmiByWordLocation);
MyClass.NextWord(SomeCharIndex, wmiByCharLocation);

However, I would recommend refactorization.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613612

Trying to use overloading here just leads to pain and suffering. Just imagine what happens when you want to use an integer literal? Or imagine you want to pass NextWord an index that is an expression, e.g. a+b. Or you pass the return value of a function. Are you going to have functions that return the various dedicated types that you propose to define? And think about any human that has to read the code. How would they be expected to determine which overload is being called?

You can avoid all the pain by giving your functions different names. Overloading is appropriate when you have multiple functions that take parameters with different types. That is not the case here. Your functions take parameters with the same type. And so your functions need different names.

Another way to organise it would be to have a single function, but to pass more to the function. Pass the index, and an enumerated type which describes how to interpret that index. You could combine those two values into a record.

Upvotes: 7

Related Questions